I want to append a string on the every line from the grep result.
For example, this command will return several lines:
ls -a | grep "filename"
For example:
filename1
filename2
filename3
filename4
How can I append a string test
on each return line using a single command? So that I get this output:
test filename1
test filename2
test filename3
test filename4
If you want to find exact matches for multiple patterns, pass the -w flag to the grep command. As you can see, the results are different. The first command shows all lines with the strings you used. The second command shows how to grep exact matches for multiple strings.
To search multiple files with the grep command, insert the filenames you want to search, separated with a space character. The terminal prints the name of every file that contains the matching lines, and the actual lines that include the required string of characters. You can append as many filenames as needed.
If you wish to append the output at the end of the file, use >> rather than > as the redirection operator. What this actually does is to start cat and grep concurrently. cat will read from q1. txt and try to write it to its standard output, which is connected to the standard input of grep .
You can do this:
ls -a | grep "filename" | perl -ne 'print "test $_"'
An alternative is to use sed (which is specifically a Stream EDitor):
ls -a | grep "filename" | sed 's/^/test /'
or
ls -a *filename* | sed 's/^/test /'
or even
ls -a | sed '/filename/bx;d;:x;s/^/test /'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With