Awk, split, print


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk, split, print
# 1  
Old 05-29-2015
Awk, split, print

How to print the split array elements in the same line with awk?
Code:
echo "1 2 3 4 /path/to/file1" | awk 'split($5, A, "/") {print $0; for (i=1; i<=length(A); i++)  print A[i]}'
echo "2 2 3 6 /longer/path/to/another/file2" | awk 'split($5, A, "/") {print $0; for (i=1; i<=length(A); i++)  print A[i]}'

What I got is:
Code:
1 2 3 4 /path/to/file1
path
to
file1
2 2 3 6 /longer/path/to/another/file2
longer
path
to
another
file2

but, what I want is:
Code:
1 2 3 4 /path/to/file1 path to file1
2 2 3 6 /longer/path/to/another/file2  longer path to another file2

Thanks!

Last edited by yifangt; 05-29-2015 at 04:19 PM..
# 2  
Old 05-29-2015
use printf to suppress carriage returns

Code:
{print $0; for (i=1; i<=length(A); i++)  print A[i]}

Code:
{printf("%s ", $0); for (i=1; i<=length(A); i++)  printf("%s ", A[i])} END{printf"\n")}'

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 05-29-2015
You can use printf "%s",arg to print without a newline.
Also the length(A) does not work, but you can take the result from the split().
Code:
echo "1 2 3 4 /path/to/file1" | awk '(n=split($5, A, "/")) {printf "%s",$0; for (i=1; i<=n; i++) printf " %s",A[i]; print ""}'
1 2 3 4 /path/to/file1  path to file1

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 05-29-2015
Another way:
Code:
awk '{n=split($NF,F,"/"); s=F[1]; for(i=2; i<=n; i++) s=FS F[i]; print $0 s}'

Or try without split() :
Code:
awk '{p=$0; gsub("/", FS, $NF); print p $NF}'

or
Code:
awk -F/ '{p=$0; $1=x; print p $0}'


Last edited by Scrutinizer; 05-29-2015 at 03:52 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 05-29-2015
Thanks a lot, to all of you!
Scrutinizer, I used the gsub() that will modify all columns, which is not so flexible as others. But yours are cool alternates without split() though!
Wait a minute, could you explain for me $1=xin this one: awk -F/ '{p=$0; $1=x; print p $0}'? Thanks!

Last edited by yifangt; 05-29-2015 at 04:14 PM..
# 6  
Old 05-29-2015
Hi, $1=x does two things:
  1. It makes the first field empty (equal to ""), by replacing it with the value of empty variable x
  2. it replaces the FS by the OFS, in this case "/" by " " (awk recomputes the record $0, when one of the fields is changed).
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 05-29-2015
Which one is the the first field you refer to?
OK, seems get it. The old FS, i.e. everything before the first slash "/".

Last edited by yifangt; 05-29-2015 at 04:34 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk split and awk calculation in the same command

I am trying to run the awk below. My question is when I split the input, then run anotherawk to perform a calculation using that splitas the input there are no issues. When I try to combine them the output is not correct, is the split not working or did I do it wrong? Thank you :). input ... (8 Replies)
Discussion started by: cmccabe
8 Replies

2. Shell Programming and Scripting

awk split help

Hello, I have the following input file: A=1;B=2;C=3;D=4 A=4;B=6;C=7;D=9 I wish to have the following output 1 2 3 4 4 6 7 9 Can awk split be used to do this? I have done this without using split, but the process is quite tedious. Any help is appreciated! (4 Replies)
Discussion started by: Rabu
4 Replies

3. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

4. UNIX for Dummies Questions & Answers

awk split

Can anybody tell me what is wrong with this ? It does not produce anyoutput. awk 'split( "this:that", arr,":")' (2 Replies)
Discussion started by: jville
2 Replies

5. Shell Programming and Scripting

AWK split

Dear colleagues! I want to create a script which will take each file from the list and then parse it filename with awk/split. I do it this way: for file in `cat /$FileListFN`; do echo `awk ' {N=split(FILENAME,FNParts,"_")} {for (i=1; i<=N; i++) ... (10 Replies)
Discussion started by: slarionoff
10 Replies

6. Shell Programming and Scripting

Split and print

I have a file with data such as: X Y Z 4 1,3,5,7, 4,6,8,10, A B C 3 2,3,4, 5,9,11, E F G 5 1,2,3,4,5, 8,9,10,11,12, Columns 1, 2 and 3 are descriptions. Column 4 tells how many numbers are in columns 5 and 6 What I'd like to do is split column 5 and column 6 by the "," and then... (3 Replies)
Discussion started by: dcfargo
3 Replies

7. AIX

split a filename and print to 2 different headings

I need help to split a filename 'a0crk_user:A0-B0123$#%test' into a0crk_user and A0-B0123 and print the output under 2 different columns namely User and Status. for eg. the output should like below: User Status ---- ------ a0crk_user A0-B0123 (3 Replies)
Discussion started by: mbak
3 Replies

8. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies

9. Shell Programming and Scripting

How do I get awk to print a " in it's print part?

The line is simple, use " '{ print $1"]"$2"\"$3THE " NEEDS TO GO HERE$4 }' I've tried \", "\, ^" and '"" but none of it works. What am I missing? Putting in the [ between $1 and $2 works fine, I just need to do the same with a ". Thanks. (2 Replies)
Discussion started by: LordJezo
2 Replies

10. Shell Programming and Scripting

split and print $PATH

Hello simple question : how can i split the $PATH by the ":" seperator with one liner ? mybe using awk is there any builtin function in awk that splits and prints the output ? thanks (2 Replies)
Discussion started by: umen
2 Replies
Login or Register to Ask a Question