Problem with blanks, shifting the row when using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with blanks, shifting the row when using awk
# 1  
Old 06-17-2015
Problem with blanks, shifting the row when using awk

Hello,
I have files with fixed length fields.
Code:
12345  12345  12345671234567        10     1234567    
12345  12345  123456  1234567        10     1234567

I want to take 1st, 3rd, 4th and 6th string.
Usually 3rd and 4th string are coming as it is on the first row /1234567 and 1234567/, then I can use
Code:
awk '{print $1,$3,$5}'

and it works. But the problem comes when the 3rd string is shorter, like it is on the second row. It is being shifted with one column.
I was reading and trying with nawk and substr, but I am not so good with Unix.
Could you please help.
Thanks in advance!
# 2  
Old 06-17-2015
Try
Code:
awk 'NF==5{$3=substr($3,1,7) " " substr($3,8); $0=$0} {print $1,$3,$4, $6}' file
12345 1234567 1234567 1234567
12345 123456 1234567 1234567


Last edited by RudiC; 06-17-2015 at 11:14 AM..
This User Gave Thanks to RudiC For This Post:
# 3  
Old 06-17-2015
Hello apenkov,

Following may help you in same.
Code:
 awk '{print $1 OFS $3 OFS $5}' FIELDWIDTHS="5 7 17 10 12" Input_file

Output will be as follows.
Code:
12345   12345671234567      1234567
12345   123456  1234567      1234567

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 06-18-2015
Thank you all!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shifting of data because of special characters

Hi Forum. I have a unique problem that I'm hoping someone can assist me. I'm generating a fixed width file and one of the output column (person_name at col. pos.#483 defined as string(36) sometimes contains french characters in the name and it causes the next column of data to shift to the... (10 Replies)
Discussion started by: pchang
10 Replies

2. Shell Programming and Scripting

awk to remove row 1 and blanks

I am trying to remove $1 along with the blank values from the file. Thank you :). file R_Index Chr Start End Ref Alt Func.IDP.refGene Gene.IDP.refGene GeneDetail.IDP.refGene Inheritence ExonicFunc.IDP.refGene AAChange.IDP.refGene avsnp147 PopFreqMax ... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

4. Programming

Shifting text

I have the following functions which shifts the text by 4 and 8 characters. How can I pass an integer for the shift so that the text is shifted accordingly? void prValue4_vd ( FILE* stream, // name of output stream const char* value, // value associated with argument... (2 Replies)
Discussion started by: kristinu
2 Replies

5. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

6. Shell Programming and Scripting

awk command : row by row merging of two files

I want to write a scrpit to merge files row wise (actually concatinating) main.txt X Y Z file 1 A B C file 2 1 2 3 now i want the script to check if the file1 is empty or not, if empty then make it like A B C 1 2 3 again to check if second file is empty if not do as done... (0 Replies)
Discussion started by: shashi792
0 Replies

7. UNIX for Dummies Questions & Answers

shifting space from one partition to other

hi My System is Sun Microsystems Inc. SunOS 5.10 Solaris Partition Info is /dev/vx/dsk/bootdg/var 27G 25G 1.2G 96% /var /dev/vx/dsk/bootdg/oravol 110G 54G 56G 49% /export/home I want to shift space 20G from /export/home to /var What should be the command ?? (2 Replies)
Discussion started by: kaushik02018
2 Replies

8. Shell Programming and Scripting

Shifting of lines in a file

Hi all, I am using the below command to shift the lines in a file which was advised by Anchal in this forum: awk -v total_records=$(cat redirects.virgin-atlantic.com.conf | wc -l) '{ if(NR>(total_records - 2)) printf "\t%s\n", $0; else print $0 }' align but I am getting the below error:... (7 Replies)
Discussion started by: Shazin
7 Replies

9. Shell Programming and Scripting

awk{FIELDWIDTHS} replacing blanks with null

Hi, I am having a file and grabbed the contents of the field according to field widths. The command i used is: awk 'BEGIN{FIELDWIDTHS="10 25 20 14 6 10"}{print$4,$5,$6}' newtext.text >test1.txt i got the output for example: val1 val2 val3 <blank> ... (3 Replies)
Discussion started by: rish_max
3 Replies

10. Shell Programming and Scripting

blanks in an awk comand

Hi, Im trying to write something that will report if a filesytem is over 80% but the problem is the output reports on a dir that is a 9%. any ideas? this is what I have ************************************************* if then param=90 else param=$1 fi df -kl | grep... (1 Reply)
Discussion started by: collie
1 Replies
Login or Register to Ask a Question