Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

swap characters on a line at a given column position

Status
Not open for further replies.

alfie002

Technical User
Mar 3, 2004
121
GB
Hello all,

This should be straight forward but I haven't been working alot with awk recently and can't my head around it.

Basically, I have a comma separate file, with a fixed numer of characters;

EG:

date_entry,id_entry,time_entry,3,6,0,6,4,7,9,0,79,2,2,4,6

This is a sample file.

I need to swap the character(s) at position 13 with those at position 11, as both characters are out of position. The key thing here is that both characters need to be swapped within the file as the file will then be processed by another system.

I don't necessarily want to create another file.

many thanks in advance

Regards

Alf
 
I need to swap the character(s) at position 13 with those at position 11
Characters or fields ?
If guess fields:
awk -F',' 'BEGIN{OFS=","}
{t=$11;$11=$13;$13=t;print}
' /path/to/input >/tmp/$$ && mv /tmp/$$ /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
hello PHV and thanks for the reply.

I have read through the script and can understand most of it. I haven't had a chance to test it yet though.
Not sure what the "If guess fields:" means, presume this is a descriptive.
Line 2 sets up the OFS and the delimitor of comma ,.
Line 3 sets up temp variable and shuffles the fields around
Line 4, not sure what this line does ?

would you please explain this line and where the new line, including the swapped variables, gets written to a file.

Many thanks for your help

Regards

Alf
 
Hello again PHV,

One thing I forgot to mention last time was that I noticed that the script runs from the command line. Would it be possible to get the script in a script file so I can run it using the -f parameter and simply called it from another location ? I think I can manage this but any ideas would be appreciated.

Thanks in advance.

Regards,

Alf
 
Line1: I guess you are talking about fields
Line2: set the Output Field Separator (OFS) to comma (,) to preserve the csv file structure
Line3: save the field 11 in t, assign to field 11 the value of field 13, assign to field 13 the saved value of field 11, write the whole new line
Line4: instruct awk which file to read and where to write and if no error replace the input by the awk's output.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
hello PHV,

I have juggled your earlier comments around and you them into a file. I having a problem with the print line.

BEGIN {OFS=","
FS=","
}
{
temp=$14
$14=$16
$16=temp
{ print testdat > /tmp/$$ && mv /tmp/$$ output}
}

Have you any ideas what might be causing the problem ?

Thanks

Alf

ps; testdat is the name of the sample data file that I am using.
 
alfie002,

If you put:

Code:
BEGIN {OFS=","
FS=","
}
{
temp=$14
$14=$16
$16=temp
{ print
}

into an awk script called switch.awk, you can call it by typing

awk -f switch.awk testdat >/tmp/$$ && mv /tmp/$$ testdat

And your output will be put back into the original file. If you want to use a different file for your output, you would just replace the second instance of testdat with the output filename of your choice.

Hope this helps.

John
 
Something like this ?
=== file: swap14and16.awk ===
BEGIN{OFS=",";FS=","}
{temp=$14;$14=$16;$16=temp;print}
=== end of file ===
Calling sequence in a shell script:
awk -f swap14and16.awk testdat > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
thanks all for your help.

Very much appreciate.

Alf
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top