awk printing leading tab in output


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk printing leading tab in output
# 1  
Old 07-29-2019
awk printing leading tab in output

The awk below executes and produces the current output. it skips the header in row 1 and prints $4,$5,$6 and then adds the header row back.
The problem is that it keeps the tailing tab and prints it in front of $1. I could add a pipe to remove the tab, but is there a better way to do it with on command? Thank you Smilie.

File tab delimeted
Code:
Total_Targets	Targets_less_than250x	Percent_more_than250x
chr11	533813	3633	215009	2077	99.034
chr11	533814	3637
chr11	533815	3637
chr11	533816	3639
chr11	533817	3640
chr11	533818	3639
chr11	533819	3643

Current output
Code:
Total_Targets	Targets_less_than250x	Percent_more_than250x
   215009 2077 99.034

Desired output
Code:
Total_Targets	Targets_less_than250x	Percent_more_than250x
   215009 2077 99.034

awk
Code:
 awk 'NR>1{$1=$2=$3=""; print $0}FNR==1' file

# 2  
Old 07-29-2019
Code:
awk 'NR>1 && NF > 3 {for (i=4; i<=NF; i++) $(i-3)=$i; NF-=3; print }FNR==1' OFS="\t" File

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 07-29-2019
Wouldn't:
Code:
awk -F'\t' 'NF>3 {print $4 FS $5 FS $6} FNR==1' File

be easier?
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 07-29-2019
Code:
awk -F'\t' 'NF>3 {print $4 FS $5 FS $6} FNR==1' File

so if I understand this the first three fields are skipped and the 4-6 are printed separated by a tab and since the first three fields are skipped so is the trailing tab in the 3rd field?

Code:
awk 'NR>1 && NF > 3 {for (i=4; i<=NF; i++) $(i-3)=$i; NF-=3; print }FNR==1' OFS="\t" File

This skips row 1 and prints after field 3 then loops through 4-6 printing them. I think $(i-3)=$i tells awk to loop through each line and the NF-=3 prints fields 4-6 and then the header is added?

I guess my question is in the first awk how does the header row get added?

Both commands work great, just trying to understand. Thank you Smilie.
# 5  
Old 07-29-2019
No statements are skipped (no next) so all lines are evaluated.

--- Post updated at 09:24 PM ---

Quote:
Originally Posted by Don Cragun
Wouldn't:
Code:
awk -F'\t' 'NF>3 {print $4 FS $5 FS $6} FNR==1' File

be easier?
Code:
NF > 6 ?  0 : 1      # (probably not)

This User Gave Thanks to rdrtx1 For This Post:
# 6  
Old 07-30-2019
Quote:
Originally Posted by cmccabe
Code:
awk -F'\t' 'NF>3 {print $4 FS $5 FS $6} FNR==1' File

so if I understand this the first three fields are skipped and the 4-6 are printed separated by a tab and since the first three fields are skipped so is the trailing tab in the 3rd field?
The awk print statement says exactly what is to be printed. Since fields 1, 2, and 3 (and the field separators following them) are not mentioned, they are not printed. The FNR==1 (with no specified action) performs the default action (print $0) when that condition evaluates to true. That condition will evaluate to true when the first line of an input file is read (i.e., when the header line of an input file is read). This was taken directly from your code

Quote:
Code:
awk 'NR>1 && NF > 3 {for (i=4; i<=NF; i++) $(i-3)=$i; NF-=3; print }FNR==1' OFS="\t" File

This skips row 1 and prints after field 3 then loops through 4-6 printing them. I think $(i-3)=$i tells awk to loop through each line and the NF-=3 prints fields 4-6 and then the header is added?

I guess my question is in the first awk how does the header row get added?
The condition and action pair:
Code:
NR>1 && NF > 3 {for (i=4; i<=NF; i++) $(i-3)=$i; NF-=3; print }

says that for each input line that has a record number (in all input files) greater than 1 AND has more than three fields, do the following. First copy each field with a field number greater than or equal to 4 to the field with field number 3 less than the field being copied. Then reduce the number of fields in the current record by 3. And, then print the updated record.

Note that since your sample input only has 3 fields on the first line, the NR>1 is a no-op and can be omitted.

The header is printed by both of our scripts exactly the same way it was printed by your script. As explained above, the FNR==1 in all of our scripts causes the first line of each input file read to be printed unchanged.

Quote:
Both commands work great, just trying to understand. Thank you Smilie.
I hope this helps you understand.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 07-30-2019
Quote:
Originally Posted by rdrtx1
... ... ...
Code:
NF > 6 ?  0 : 1      # (probably not)

Hi rdrtx1,
I guess I don't understand why you posted this code. With the sample input provided, the above code copies the input file to the output (since no input line has more than 6 fields). Smilie

The command:
Code:
awk 'NF > 3 || NR == 1' File

would print just the requested lines but does not discard the first three fields on lines with 6 fields.
Note well: This post has been updated. I got the logic backwards when I first read the script. The first paragraph now correctly specifies how the quoted code behaves. I apologize for any confusion this may have caused.

Last edited by Don Cragun; 07-30-2019 at 02:10 AM.. Reason: Fix description of quoted code behavior.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk printing output to new line

Hi I have a file profile.txt with the below input: {"atgUserId":"736f14c4-eda2-4531-9d40-9de4d6d1fb0f","firstName":"donna","lastName":"biehler","email":"schoolathome42@live.com","receiveEmail":"y es"}, {"atgUserId":"c3716baf-9bf8-42da-8a44-a13fff68d20f","firstName":"Gilberto... (6 Replies)
Discussion started by: ankur328
6 Replies

2. Shell Programming and Scripting

awk to add tab to output of both conditions

In the below awk written by @RavinderSingh13 I have added a few lines and am trying to have the output be tab-delimited. The input is space-delimeted and the portion in bold seems to add a tab to the Not found but not the found. Thank you :). file1 One 1 Two 2 Three 3 file2 One 1... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk output is space delimeted not tab delimeted

In the below awk the output is space delimited, but it should be tab delimited. Did I not add the correct -F and OFS? Thank you :). The input file are rather large so I did not include them, but they are tab-delimeted files as well. awk awk -F'\t' -v OFS='\t' 'FNR==1 { next } > ... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

awk output seperated by tab

I am just trying to output the below awk separated by tabs. Thank you :). awk (added OFS as an attempt to itroduce tabs) awk '{split($5,a,"-"); OFS='\t' print $1,$2,$3,a}' file.bed > test.bed The awk runs and produces all the data in 1 field instead of 4 fields. current output ... (2 Replies)
Discussion started by: cmccabe
2 Replies

5. Shell Programming and Scripting

Help! Printing out CSV output from awk Pattern Match

Hi, I need to search for a word using Awk and print out the line the word is in and every line after the search phrase until I hit this #------------. Then I need to send it to a csv file. So basically the input file format is like this:... (1 Reply)
Discussion started by: An0mander
1 Replies

6. Shell Programming and Scripting

awk and leading zeroes

I have the following script that renames filenames like: blah_bleh_91_2011-09-26_00.05.43AM.xls and transforms it in: 91_20110926_000543_3_blih.xls for a in *.xls; do b="$(echo "${a}" | cut -d '_' -f4)" dia=`echo ${b} | cut -c9-10` mes=`echo ${b} | cut -c6-7` anio=`echo ${b} | cut -c1-4`... (4 Replies)
Discussion started by: Tr0cken
4 Replies

7. Shell Programming and Scripting

printing two values with TAB in between

Dear friends, I want to print variables' values in certain format where space between two values of variables is "a tab" I tried where I provided "tab" between two varibales. But when it print values on screen its giving me output without spaces in two values. Request you to help me in... (7 Replies)
Discussion started by: anushree.a
7 Replies

8. Shell Programming and Scripting

output - tab formatted - awk

Dear All, Good Day. I would like to hear your suggestions for the following problem: I have a file with 5 columns with some numbers in 16 lines as shown below. Input file: Col 1 Col 2 Col 3 Col 4 Col 5 12 220 2 121 20 234 30 22 9... (3 Replies)
Discussion started by: Fredrick
3 Replies

9. Shell Programming and Scripting

printing portion of the output usind sed/awk

friends, i am a newbie in scripting. could someone help me in selecting only the last column of below ps command output ? mqm 14 16466 0 Sep 15 ? 0:01 /opt/mqm/bin/runmqlsr -r -m QMGR.INBOUNDSSL -t TCP -p 1415 -i 5.1.26.5 mqm 12 16700 0 Sep 15 ? 0:00... (4 Replies)
Discussion started by: unahb1
4 Replies

10. UNIX for Dummies Questions & Answers

Printing problem - Vertical Tab

At work, when I issue: lpr -P$PRINTER -h filename the file always prints a line then does a vertical tab off that first line instead of starting the new line at left. It does this with every file I've tried, and I can't find any options that are set that would make things print this way. I... (2 Replies)
Discussion started by: scaredinjuly
2 Replies
Login or Register to Ask a Question

Featured Tech Videos