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!
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:
Desired Output:
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:
And in my action block, I have:
My action block seems to be where i'm running into a problem at.
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:
which produces the output:
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)