I would like to print odd lines (1,3,5,7..) without any change, but even lines (2,4,6,8) process with pipeline beginning with grep. I would like to write everything to new file (odd lines without any change and new values for even lines).
I know how to print every other line in awk:
awk ' NR % 2 == 1 { print; } NR % 2 ==0 {print; }' file.fasta
However, for even lines, I dont want to use {print; }
but I want to use my grep pipeline instead.
An advice will be appreciated. Thanks a lot.
Using sed:n command in sed prints the current line(if -n is not present) and reads the next line in the buffer. Since -n switch is present, the n command does not print.
Print every alternate line:n command prints the current line, and immediately reads the next line into pattern space. d command deletes the line present in pattern space. In this way, alternate lines get printed.
It means "Use the second argument if the first is undefined or empty, else use the first". The form "${2-${1}}" (no ':') means "Use the second if the first is not defined (but if the first is defined as empty, use it)". Copy link CC BY-SA 2.5.
One such variation is the “For each line in file” which is responsible for reading all the lines in a file. In this article, we will talk about the methods of using “for each line in file” in Bash. Note: The methods shown below have been demonstrated on Ubuntu 20.04. However, they will also work well with any other Linux distribution.
Solution: A simple way to process every line in a text file is to use a Unix/Linux while loop in combination with the Linux cat command, like this: Now all you have to do is put some code inside the while loop.
Now run the Bash script again via the terminal with the bash command followed by the Bash file name. This time when the Bash script will run, it will create a new text file whose name in this case is NewFile.txt. The output shown in the image below will assure you that all the read lines have been copied to the new text file.
Then in the “do-done” block, we have simply displayed all these lines on the terminal by making use of the echo command. When this command will make the bash script to execute, then you will be able to see all the lines of your text file on your terminal as shown in the following image:
If you're planning to do a simple grep
, you can do away with the additional step and do the filtering within awk itself, e.g.:
awk 'NR % 2 {print} !(NR % 2) && /pattern/ {print}' file.fasta
However, if you intend to do a lot more then, as chepner already pointed out, you can indeed pipe from inside awk. For example:
awk 'NR % 2 {print} !(NR % 2) {print | "grep pattern | rev" }' file.fasta
That opens a pipe to the command "grep pattern | rev"
(note the surrounding quotes) and redirects the print output to it. Do note that the output in this case may not be as you might expect; you will end up with all odd lines being output first followed by the output of the piped command (which consumes the even lines).
(In response to your comments) to count the number of chars in each even line, try:
awk 'NR % 2 {print} !(NR % 2) {print length($0)}' file.fasta
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