awk related question (for loop) difficult scenario


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk related question (for loop) difficult scenario
# 1  
Old 08-09-2012
awk related question (for loop) difficult scenario

Code:
VARA='hello|welcome|gone|fantastic|superb|nicecar'

if VARA contains a pipe "|", i want the contents of VARA to be tranformed to:

Code:
VARA="(hello) (welcome) (gone) (fantastic) (superb) (nicecar)"

so that, when i echo the contents of $VARA like this:

Code:
echo "$VARA"

or like this:

Code:
print "$VARA"

i should get:

Code:
(hello) (welcome) (gone) (fantastic) (superb) (nicecar)

I would prefer to use awk for this.

Shell: bash
OS: Linux or SunOS
# 2  
Old 08-09-2012
You've had quite a number of posts already and have been helped in the past.
Could show exactly what you've tried so far and where exactly you're stuck, please.
# 3  
Old 08-09-2012
Code:
VARA=`echo $VARA | awk -F"|" '{for(i=1;i<=NF;i++){$i="("substr($i,1)")"};print}'`

# 4  
Old 08-09-2012
this scenario is nothing like what i've been help with in the past.

i'm not stuck anywhere because i dont know how to do this in awk which is what i prefer...as i view it to be significantly more efficient than shell.

this is about the most i've been able to come up with. and clearly, this isn't producing the output i want.

Code:
VARA='hello|welcome|gone|fantastic|superb|nicecar'

VARB=$(echo $VARA | sed 's~|~) (~g')

echo $VARB:

hello) (welcome) (gone) (fantastic) (superb) (nicecar

# 5  
Old 08-09-2012
Code:
VARA=`echo $VARA | awk -F"|" '{for(i=1;i<=NF;i++){$i="("substr($i,1)")"};print}'`

This User Gave Thanks to raj_saini20 For This Post:
# 6  
Old 08-09-2012
you're pretty close - you need to take care of the first and the last fields:
Code:
echo 'hello|welcome|gone|fantastic|superb|nicecar' | sed 's/|/) (/g;s/^/(/;s/$/)/'
or
echo 'hello|welcome|gone|fantastic|superb|nicecar' | awk -F'|' '{$0="(" $0 ")"}$1=$1' OFS=') ('


Last edited by vgersh99; 08-09-2012 at 08:52 AM..
This User Gave Thanks to vgersh99 For This Post:
# 7  
Old 08-09-2012
Quote:
Originally Posted by vgersh99
you're pretty close - you need to take care of the first and the last fields:
Code:
echo 'hello|welcome|gone|fantastic|superb|nicecar' | sed 's/|/) (/g;s/^/(/;s/$/)/'
or
echo 'hello|welcome|gone|fantastic|superb|nicecar' | awk -F'|' '{$0="(" $0 ")"}$1=$1' OFS=') ('


thank you!

would i be asking too much if i asked that you please explain the awk statement for me?

it does what i want but i'm just curious as to how it does it? i can't read it. can you please break it down for me?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Basic loop awk/shell script question..

Hi, Sorry if this is a newbie question. I guess you can use either awk or shell script for this sequence of operations, but knowing very little about either of them I'm not sure how I should try to write this. The basic objective is to copy certain files that are scattered all over my... (10 Replies)
Discussion started by: pc2001
10 Replies

2. Shell Programming and Scripting

awk related question

awk -F ";" 'FNR==NR{a=$1;next} ($2 in a)' server.list datafile | while read line do echo ${line} done when i run the above, i get this: 1 SERVICE NOTIFICATION: nagiosadmin skysmart-01.sky.net .... instead of: SERVICE NOTIFICATION: nagiosadmin skysmart-01.sky.net .... can... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. UNIX for Dummies Questions & Answers

Question related to 'ps'

If I run a script called 'abc.sh' and then execute the following : ps -ef | grep 'abc.sh' I always get two rows of output, one for the executing script, and the other for the grep command that I have triggered after the pipe. Questions: Why does the second row turn up in the results. My... (10 Replies)
Discussion started by: jawsnnn
10 Replies

4. Shell Programming and Scripting

A simple loop made difficult WTH

Can anyone assit me in completing this simple looping problem allow the user to enter as many number as they want by using a loop. The program will keep asking the user for additional numbers until the user enters 9999. Once the user enters 9999, exit the loop and display a message telling them... (5 Replies)
Discussion started by: campanellisj
5 Replies

5. Shell Programming and Scripting

awk related question

awk "/^<Mar 31, 2012 : /,0" /app/blah.log can someone please help me figure out why the above command isn't pulling anything out from the log? basically, i want it to pull out all records, from the very first line that starts with the date "Mar 31, 2012" and that also has a time immediately... (4 Replies)
Discussion started by: SkySmart
4 Replies

6. Shell Programming and Scripting

pls help kinda difficult question for a beginner

1. Create a directory script (project_dir.sh) · Write a script in your home directory to create a subdirectory called “PROJECT<99>” within your home directory on the Ubantu/Linux server. <99> is your project number (e.g. 01,02,03, ... 15). · The script must test whether... (1 Reply)
Discussion started by: bugenhagen_
1 Replies

7. Shell Programming and Scripting

Question related to sed or awk or perl

Hi All, I have a big file of 100,000 lines with the following format: 1,736870,736870,1,2,5,547,1253889535,1253889775,240,30,152.163.141.8,US,0,00,-g 1253889445 1,881246,881246,1,2,6,402,1253889535,1253889775,240,30,152.163.141.8,US,0,00,-g 1253889445... (4 Replies)
Discussion started by: toms
4 Replies

8. UNIX for Dummies Questions & Answers

so difficult question about using grep

en...how to grep some words from some file, the goal is that, we donot want the words are exactly 9 charactor, and want to grep from some words that are longer than 9 and it contain a substring that with 9 different charactors (2 Replies)
Discussion started by: shrimpy
2 Replies

9. UNIX for Dummies Questions & Answers

Silly question on printing for loop in AWK

Hi, One silly question. I would like to add statement like below and append to a file. I used the below code; however, it does not work. Can anyone please tell me what mistakes I have made? awk ' { for (i=1;i<=563;i++) print i }'>>output.txt Thanks. -Jason (1 Reply)
Discussion started by: ahjiefreak
1 Replies

10. UNIX for Advanced & Expert Users

Weird scenario with Awk

Guys, this one is rather odd. I've got an array of numbers, and I'm trying to select only the records with the string "Random" in the 4th column. I'm using awk in this format: awk '{ if (( $6 -eq Random )) print $0 }' For some odd reason, this is simply giving me the list of all the entries... (4 Replies)
Discussion started by: Khoomfire
4 Replies
Login or Register to Ask a Question