Home
Man
Search
Today's Posts
Register

This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in the UNIX for Beginners Questions & Answers forum.

change field separator only from nth field until NF

Tags
beginners

 

 
Thread Tools Search this Thread
# 1  
Old 08-17-2012
change field separator only from nth field until NF

Hi !

input:
Code:
111|222|333|aaa|bbb|ccc
999|888|777|nnn|kkk
444|666|555|eee|ttt|ooo|ppp

With awk, I am trying to change the FS "|" to "; " only from the 4th field until the end (the number of fields vary between records).

In order to get:
Code:
111|222|333|aaa; bbb; ccc
999|888|777|nnn; kkk
444|666|555|eee; ttt; ooo; ppp

I tried something like:
Code:
gawk 'BEGIN{FS=OFS="|"}{for(i=5; i<=NF; i++) $4 = $4 ($4?"; ":"")$i}1' input

It works for the 4th field, but it also prints the original fields from $5 to $NF after:
Code:
111|222|333|aaa; bbb; ccc|bbb|ccc
999|888|777|nnn; kkk|kkk
444|666|555|eee; ttt; ooo; ppp|ttt|ooo|ppp

Is there any way to to create an array from $4 until $NF without knowing the number of fields (a[$4...$NF])???

Thanks for your help !
# 2  
Old 08-17-2012
I think this is what you are looking for:

Code:
awk -F '|' '
    {
        for( i = 1; i < NF; i++ )
            printf( "%s%s", $(i), i > 3 ? ";" : "|" );
        printf( "%s\n", $(i) );
    }
' input-file >output-file

The Following User Says Thank You to agama For This Useful Post:
beca123456 (08-19-2012)
 

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Inserting a field without disturbing field separator on other fields am24 Shell Programming and Scripting 15 05-16-2016 04:16 AM
Field separator m6248m Shell Programming and Scripting 11 06-10-2015 03:33 PM
Replace a value of Nth field of nth row deepakwins Shell Programming and Scripting 1 07-25-2014 05:11 AM
awk field separator help - rveri Shell Programming and Scripting 9 08-29-2013 01:39 AM
Strings as Field separator pamu Shell Programming and Scripting 3 06-21-2012 04:22 AM
Array and field separator Dedalus Shell Programming and Scripting 3 08-31-2011 10:01 AM
echo field separator locoroco Shell Programming and Scripting 3 08-08-2011 09:29 PM
Field separator X'1F' rohan10k Shell Programming and Scripting 2 05-24-2011 10:17 AM
awk, comma as field separator and text inside double quotes as a field. kevintse Shell Programming and Scripting 8 11-15-2010 06:31 PM
Field separator in awk aoussenko Shell Programming and Scripting 2 03-29-2010 01:59 PM
dynamically change awk Field Separator FS satnamx Shell Programming and Scripting 2 08-24-2009 07:51 PM
field separator in Perl ahsog Shell Programming and Scripting 38 04-17-2009 09:30 PM
Field separator Ques. yahyaaa Shell Programming and Scripting 6 09-16-2008 02:54 PM
Change field separator of grep from : to space Jenny.palmy UNIX for Dummies Questions & Answers 2 06-09-2008 04:16 AM
field separator as regexp Abhishek Ghose Shell Programming and Scripting 2 02-15-2006 08:49 PM


All times are GMT -4. The time now is 10:47 AM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password