LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-23-2011, 04:58 AM   #1
froggins
LQ Newbie
 
Registered: Feb 2011
Posts: 2

Rep: Reputation: 0
Smile simplifying awk command


Hello,
I have just started using linux but fast becoming a convert.

I have a text file with 8,130 rows and 106 columns arranged as follows:

1 2 3 4 1 2
3 4 1 2 3 4


I want to re-arrange it so that it looks like this:

1/2 3/4 1/2
3/4 1/2 3/4

I have so far got

awk '{print $1"/"$2" "$3"/"$4" "$5"/"$6" "$7"/"$8" "105"/"106}' > output.txt

But I thought this was rather inelegant and wondered if I can write a script that will repeat the same operation on adjacent columns.

Apologies if this is a simple question.

Thanks!

Kirsten
 
Old 02-23-2011, 07:45 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
I used 'sed'...:

Code:
sed "s/\([0-9]\) \([0-9]\)/\1\/\2/g" input.txt > output.txt
This replaces every occurrence of
Code:
\([0-9]\) \([0-9]\)
(ie. two digits separated by a space - the \( and \) mean we can recall the digits we matched later on)

with
Code:
\1\/\2
(ie. the two digits we matched before, now separated by a slash. The slash needs to be escaped to prevent sed interpreting it as the end of the pattern)

Hope this helps,

Last edited by Snark1994; 02-23-2011 at 07:47 AM.
 
1 members found this post helpful.
Old 02-23-2011, 08:26 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
I agree that sed is a more compact solution here, but would add that the use of -r alleviates all the escaping so it can be a little clearer:
Code:
sed -r 's@([0-9]+) ([0-9]+)@\1/\2@g' file
Edit: Just thought of a funky awk solution:
Code:
awk 'ORS=(NR % 2)?"\\":RT' RS="[ \n]" file

Last edited by grail; 02-23-2011 at 08:29 AM.
 
1 members found this post helpful.
Old 02-23-2011, 08:44 AM   #4
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 45
Code:
$ awk '{for(i=2;i<=NF;i+=2)$i="/"$i}{gsub(/ \//,"/")}1' file
 
1 members found this post helpful.
Old 02-23-2011, 09:21 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Or even:
Code:
awk '$0 = gensub(/(.) (.)/,"\\1\\\\2","g")' file
Equivalent to sed
 
1 members found this post helpful.
Old 02-24-2011, 05:33 AM   #6
froggins
LQ Newbie
 
Registered: Feb 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Sed/Awk - thanks!

Dear all,

Wow. Thank you for all these options. I have learned a lot from trying them all.

Kirsten
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Awk command help Guilty1682 Linux - Newbie 10 02-21-2011 09:24 PM
[SOLVED] awk command help fredora Programming 4 11-25-2010 06:23 PM
About awk command incomingid Linux - Newbie 5 04-01-2009 11:44 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 05:11 PM
the 'awk' command. iconicmoronic Linux - Newbie 2 04-08-2007 01:29 AM


All times are GMT -5. The time now is 11:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration