LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-19-2014, 11:00 AM   #1
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Rep: Reputation: 16
every fields make "-" delimited using awk


I've to make a space seperated data information to "-" delimited. If the fields were fixed I can use:
Code:
awk '{print $1"-"$2"-"$3"-"$4"-"}' final.txt
having four fields.

but how can I do the same when the fields are variable in every rows.
Suppose:

Quote:
AAA BBB CCC

UUU TTT III OOO

RRR SSS PPP GGG HHH

BBB MMM
required o/p:

Quote:
AAA-BBB-CCC

UUU-TTT-III-OOO

RRR-SSS-PPP-GGG-HHH

BBB-MMM
 
Old 09-19-2014, 11:08 AM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

How about this:

Code:
$ cat infile
AAA BBB CCC

UUU TTT III OOO

RRR SSS PPP GGG HHH

BBB MMM 
$ sed 's/ \+\b/-/g' infile
AAA-BBB-CCC

UUU-TTT-III-OOO

RRR-SSS-PPP-GGG-HHH

BBB-MMM
 
Old 09-19-2014, 11:12 AM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
How about this
Code:
awk '{printf $1}{for(i=2;i<=NF;i++)printf "-"$i}{printf "\n"}'
Code:
$ cat final.txt
AAA BBB CCC

UUU TTT III OOO

RRR SSS PPP GGG HHH

BBB MMM
$ awk '{printf $1}{for(i=2;i<=NF;i++)printf "-"$i}{printf "\n"}' final.txt
AAA-BBB-CCC

UUU-TTT-III-OOO

RRR-SSS-PPP-GGG-HHH

BBB-MMM
edit: beat by the sed...
 
Old 09-19-2014, 11:38 AM   #4
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Assuming one space:
Code:
bash-4.2$ cat tt
AAA BBB CCC

UUU TTT III OOO

RRR SSS PPP GGG HHH

BBB MMM
bash-4.2$ tr ' ' '-' < tt
AAA-BBB-CCC

UUU-TTT-III-OOO

RRR-SSS-PPP-GGG-HHH

BBB-MMM
If you have more than one space in between you can use the 'tr -s' option to squeeze repeats.

Code:
bash-4.2$ cat tt
AAA BBB CCC

UUU TTT   III OOO

RRR SSS PPP GGG HHH

BBB   MMM
bash-4.2$ tr -s ' ' '-' < tt
AAA-BBB-CCC

UUU-TTT-III-OOO

RRR-SSS-PPP-GGG-HHH

BBB-MMM
 
2 members found this post helpful.
Old 09-20-2014, 01:58 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Code:
awk '$1=$1' OFS="-" file
 
  


Reply

Tags
awk



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
kernel make error: "vmlinux.o no such file ..." and make -d "Reaping losing ..." micscherer Linux - Kernel 3 06-12-2012 07:29 AM
[SOLVED] Grabbing the last field of a delimited entry without using "awk" or "sed" drandre Linux - Newbie 17 04-18-2012 10:54 PM
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
printing hh in hh:mm using "awk '{FS=":";print $1}'" misses first line of output!! mayankmehta83 Linux - Newbie 2 12-03-2009 02:55 AM
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 10:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:00 PM.

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
Open Source Consulting | Domain Registration