Help with splitting and rearranging a field in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with splitting and rearranging a field in awk
# 1  
Old 10-22-2013
Help with splitting and rearranging a field in awk

Please help! I figured I would take this one to the experts. I'm working with a field that contains contents such as:
LastName FirstName

For example:
Smith John

I'm trying to take this field and split it so that it is two separate fields (first name and last name). I then need to print these two fields in the order of FirstName LastName. I know how to do this using printf, I just don't know how to split the field into two usable fields. Please help!

Thank you so very much.
# 2  
Old 10-22-2013
Code:
echo "Smith John" | awk '{print $2, $1}'

# 3  
Old 10-22-2013
This example appears to definitely rearrange the name, but i'm still having issues with my AWK script.

I'm writing this in a script, rather than simply trying to perform it in one line. How can I tell my AWK script to take say field 3 and split it into two usable fields? Because then i'm needing to print those two fields, much like you did.

Edit: Also, please keep in mind that these are fields with a colon delimiter.


I figured I would help clarify my question a little bit..

Input:
Code:
UserID1:Name:UserID2             
x45:Smith John:455
x46:Smith April:456

Desired Output:
Code:
First Name    Last Name   UserID2
John            Smith         455
April            Smith         456

I'm printing the new headers of First Name / Last Name using a formatted printf statement. I want to then print the names (which I need to separate the field into two separate usable fields first) using the same formatted printf statement.

In my begin block, I have:
Code:
BEGIN {
format1="%-20s\t%-20s\t%-8s\t \r\n";
printf format1, "First Name","Last Name";

And in my action block, I have:
Code:
printf format1, $2

My action block seems to be where i'm running into a problem at.

Thanks again!

Last edited by IX3R0XI; 10-22-2013 at 11:36 PM..
# 4  
Old 10-23-2013
You can explicitly split into an array
Code:
split($2,a," ")
printf format1,a[1],a[2]

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 10-23-2013
That worked perfectly, thank you so much!
# 6  
Old 10-23-2013
The code that you showed us clearly did NOT produce the headers you said you want. Both of your printf statements are missing arguments. The format string you're using requires three arguments (in addition to the format argument); your first printf has two, your second printf has one.

Your format statement is strange. Ending a heading with a tab followed by a space followed by a carriage return and finally the newline character seem especially strange.

You have a field width on the last field that will be printed left justified which will add paces to the end of the ID which will then followed by the tab, space, CR, and NL. Why? I have stripped out some of this, since I didn't see the point. Obviously you can add them back if there is a reason for them. If your input is on a Windows system and you want the output to also contain CR/NL at the ends of all lines instead of just the normal UNIX-like system NL line terminator, the formats I've used here will copy the carriage returns from the last field of each input line to the output.

Most formats use tabs or a fixed width (space filled) field to line up output fields. You use both. I left that in the format below, but you should think about whether this is really what you want.

I'm guessing that the following:
Code:
awk -F '[ :]' '
BEGIN { format1="%-20s\t%-20s\t%s\n" }
FNR == 1 {
        printf(format1, "First " $2, "Last " $2, $3)
        next
}       
{       printf(format1, $3, $2, $4) }' Input

which produces the output:
Code:
First Name          	Last Name           	UserID2
John                	Smith               	455
April               	Smith               	456

is close to what you want (based on your given format string) even though the spacing is very different from the output you said you wanted.

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update field using matching value in file1 and substring in field in file2

In the awk below I am trying to set/update the value of $14 in file2 in bold, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

awk to adjust coordinates in field based on sequential numbers in another field

I am trying to output a tab-delimited result that uses the data from a tab-delimited file to combine and subtract specific lines. If $4 matches in each line then the first matching sequential $6 value is added to $2, unless the value is 1, then the original $2 is used (like in the case of line... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Help in splitting Sub Fields and compare with other field

Hi All, We are trying to pull out data from below table, the table contains four fields and out of which last two fields are having sub-fields with delimiter $, we want to identify number "1" position in the 3rd field and from 4th field need to extract the information from the same position. ... (4 Replies)
Discussion started by: rramkrishnas
4 Replies

4. Shell Programming and Scripting

Doubt with rearranging file through awk

Filename1.xml NO 2012-11-16 02:00:27 20121115/pathname/ asia Filename1.rec YES 2012-11-16 01:20:24 20121115/pathname asia FIleName2.xml YES 2012-11-16 01:20:25 20121115/pathaname asia if the file content are... (6 Replies)
Discussion started by: manas_ranjan
6 Replies

5. Shell Programming and Scripting

Rearranging into new columns (awk?)

Hi experts, I've used several solutions from this forum to delete nonsense and rearrange data in the project file I'm working on. I'm hoping you guys can give me some tips on further rearranging the data (I've seen a few solutions by searching, but one specific item has me stumped, which is only... (5 Replies)
Discussion started by: coryvp
5 Replies

6. Shell Programming and Scripting

Splitting large file and renaming based on field

I am trying to update an older program on a small cluster. It uses individual files to send jobs to each node. However the newer database comes as one large file, containing over 10,000 records. I therefore need to split this file. It looks like this: HMMER3/b NAME 1-cysPrx_C ACC ... (2 Replies)
Discussion started by: fozrun
2 Replies

7. Shell Programming and Scripting

Splitting record into multiple records by appending values from an input field (AWK)

Hello, For the input file, I am trying to split those records which have multiple values seperated by '|' in the last input field, into multiple records and each record corresponds to the common input fields + one of the value from the last field. I was trying with an example on this forum... (4 Replies)
Discussion started by: imtiaz99
4 Replies

8. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

9. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

10. Linux

awk/sed for splitting a field into two

I have a tab delimitted dataset with 4 fields. I like to split the second field into two, and have 5 fields. I like to remove the "-" sign when I get a new fiel. would you help? It is like: 1223 100-5 rr dd I need it like: 1223 100 5 rr dd (2 Replies)
Discussion started by: sire
2 Replies
Login or Register to Ask a Question