Ask Your Question
0

Bash: How can I grep all Lines with alphanumeric Character from a file and print them as Python Array?

asked 2023-04-07 22:30:01 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-04-07 22:30:06 +0000

qstack gravatar image

Here's an example of how to use grep and awk to filter alphanumeric lines from a file and print them as a Python array:

grep -E '^[a-zA-Z0-9]+$' file.txt | awk '{printf("\"%s\", ", $0)}' | sed 's/, $/\n/' | awk '{printf("[%s]", $0)}'

This command uses grep to filter the alphanumeric lines from file.txt by searching for lines that match the regular expression ^[a-zA-Z0-9]+$. The output of grep is then piped to the first awk command, which adds double quotes around each line to make it a valid string in Python. The output of this command is then piped to the second awk command, which uses printf to print the lines as a Python array in the format [ "line1", "line2", ... ].

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

1 follower

Stats

Asked: 2023-04-07 22:30:01 +0000

Seen: 33 times

Last updated: Apr 07 '23