LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
LinkBack Search this Thread
Old 07-26-2010, 04:13 AM   #1
BerzinTehvs
Member
 
Registered: Mar 2005
Location: Latvia
Distribution: Slackware
Posts: 47

Rep: Reputation: 15
awk and sorting


Hi!

I have in bash script the following line:
Code:
TMP1=`echo $TMPO | awk '{FS=", "} {for (i = 1; i <= NF; i++) print $i}' | sort -z`
which sorts words within string, echo $TMP1 shows that everything is OK, but when i try to :
Code:
Output=`echo $line | awk -F $DELIM  '{OFS=FS; $1="'"$TMPW"'"; $4="'"$TMP1"'" } {print}'`
I get:
awk: {OFS=FS; $1="word1"; $4="word2,
awk:_________________________^ unterminated string

(underline put by me for positioning)
Whats wrong with TMP1? If i manipulate TMP1 and insert into Output without sorting, everything works.

Last edited by BerzinTehvs; 07-26-2010 at 04:47 AM.
 
Old 07-26-2010, 04:23 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
use the $() syntax, and when declaring FS, do it inside BEGIN{} block, or use -F. Quote your variables. Use \047 as the single quote in awk.

Code:
TMP1=$(echo $TMPO | awk 'BEGIN{FS=", "}{for (i = 1; i <= NF; i++) print $i}' | sort -z)
Output=$(echo "$line" | awk -F"$DELIM" '{ $1="\047"$TMPW"\047"; $4="\047"$TMP1"\047" } {print}' OFS="$DELIM")
 
Old 07-26-2010, 04:46 AM   #3
BerzinTehvs
Member
 
Registered: Mar 2005
Location: Latvia
Distribution: Slackware
Posts: 47

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ghostdog74 View Post
use the $() syntax, and when declaring FS, do it inside BEGIN{} block, or use -F. Quote your variables. Use \047 as the single quote in awk.

Code:
TMP1=$(echo $TMPO | awk 'BEGIN{FS=", "}{for (i = 1; i <= NF; i++) print $i}' | sort -z)
Output=$(echo "$line" | awk -F"$DELIM" '{ $1="\047"$TMPW"\047"; $4="\047"$TMP1"\047" } {print}' OFS="$DELIM")
the code doesnt' give whats expected:

1. in TMP1 all delimiters are lost, wordsa are separated by spaces, not original ", ", which was in my example;
2. Output somehow becomes trippled original string represented by $line.

I'd be happy understanding why TMP1 acquired in the original way gives that error. (actually, if I use the TMP1 acquired in the way advised for insertion into Output in original way, I get the same error, so I assume everything hides within Output code line)

Last edited by BerzinTehvs; 07-26-2010 at 04:58 AM.
 
Old 07-26-2010, 08:22 AM   #4
BerzinTehvs
Member
 
Registered: Mar 2005
Location: Latvia
Distribution: Slackware
Posts: 47

Original Poster
Rep: Reputation: 15
Hi,
I am now so far:
Code:
TMP1=`echo $TMPO | awk -F ", " ' {RS=", "; n=split($0,arr); asort(arr); for (i = 1; i <= n; i++) print arr[i]}'`
the only problem is that I am not getting words separated by ", ".

if i modify it like this:
Code:
TMP1=`echo $TMPO | awk -F ", " ' {RS=", "; n=split($0,arr); asort(arr); for (i = 1; i <= n; i++) print arr[i]", "}'`
I get one unnecessary separator at the end.

How could I improve it? Adding ORS to the script gives the same extra separator at the end.

Last edited by BerzinTehvs; 07-26-2010 at 08:30 AM.
 
Old 07-26-2010, 08:30 AM   #5
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
EDIT - Sorry, I had a typo - back in a minute

Last edited by GrapefruiTgirl; 07-26-2010 at 08:31 AM.
 
Old 07-26-2010, 08:36 AM   #6
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
Code:
 echo "hello, goodbye, rotten, apple, core" | \
awk -F ", " ' {RS=", "; n=split($0,arr); asort(arr); for (i = 1; i < n; ++i) print arr[i]", "; print arr[i]}'
There - sorry about that.
 
Old 07-26-2010, 11:43 AM   #7
BerzinTehvs
Member
 
Registered: Mar 2005
Location: Latvia
Distribution: Slackware
Posts: 47

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by GrapefruiTgirl View Post
Code:
 echo "hello, goodbye, rotten, apple, core" | \
awk -F ", " ' {RS=", "; n=split($0,arr); asort(arr); for (i = 1; i < n; ++i) print arr[i]", "; print arr[i]}'
while the example runs smoothly, in my script the resulting string anyway gets an extra comma at the end (and without ORS it crashes next awk with message about unterminated string)

so, I killed the last comma with
Code:
TMP2=`echo $TMP1 | sed '$s/.$//'`

Last edited by BerzinTehvs; 07-26-2010 at 11:46 AM.
 
Old 07-26-2010, 11:46 AM   #8
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
Could you show a sample of exactly what the input file looks like (what it contains) as well as the exact output you desire? I'll have another look at it
 
Old 07-31-2010, 08:44 AM   #9
BerzinTehvs
Member
 
Registered: Mar 2005
Location: Latvia
Distribution: Slackware
Posts: 47

Original Poster
Rep: Reputation: 15
see this thread:
http://www.linuxquestions.org/questi...locale-822982/
 
  


Reply

Tags
awk, string


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
Help with pattern matching, sorting data with awk/gawk or perl placem Programming 2 09-11-2008 02:26 PM
awk sort function not sorting from lowest to highest. skuz_ball Programming 4 08-09-2008 12:20 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
Some comments on awk and awk scripts makyo Programming 4 03-02-2008 05:39 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration