Getting values in pattern /val1/val2/val3/...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting values in pattern /val1/val2/val3/...
# 1  
Old 02-22-2012
Getting values in pattern /val1/val2/val3/...

I have a string containing /val1/val2/val3/... and want to get all the fields and determine how many there are, so that I can create the string
Code:
-k val1n -k val2n -k val3n

Example:

Code:
set str = "/5/6/7"
-k 5n -k 6n -k 7n

Code:
 set str = "/7/8/9/23"
-k 7n -k 8n -k 9n -k 23n

# 2  
Old 02-22-2012
Try this awk program:
Code:
~/unix.com$ echo '/7/8/9/23' | awk -F\/ '{i=1;while(i++<NF)s=s"-k "$i"n "}END{print s}'

This User Gave Thanks to tukuyomi For This Post:
# 3  
Old 02-22-2012
Dear friend ,
Can you please let us know if the below command line satisfies your requirement ?


Code:
echo "/5/6/7" | awk -F "/" '{for (i=1;i<=NF;i++){if (i==1){continue}printf " -k " $i "n"}}'

This User Gave Thanks to codemaniac For This Post:
# 4  
Old 02-22-2012
Dear friend ,
Can you please let us know if the below command line satisfies your requirement ?


Code:
echo "/5/6/7" | awk -F "/" '{for (i=1;i<=NF;i++){if (i==1){continue}printf " -k " $i "n"}}'

# 5  
Old 02-23-2012
I need to have a bit of modification in the input to this one

Code:
5/6/7

---------- Post updated at 06:03 PM ---------- Previous update was at 06:03 PM ----------

With
Code:
/5/6/7

things work fine
# 6  
Old 02-23-2012
How about:
Code:
$ echo "5/6/7" | sed -e 's;^;/;' -e 's;/\([^/]*\);-k \1n ;g'
-k 5n -k 6n -k 7n

or
Code:
$ echo "5/6/7" | awk -F/ '{OFS="n -k "; $1="-k "$1; print $0"n"} '
-k 5n -k 6n -k 7n

This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 02-23-2012
That sed gave me the creeps. Smilie
I'll stick with your awk.

Last edited by kristinu; 02-23-2012 at 08:13 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find specific pattern and change some of block values using awk

Hi, Could you please help me finding a way to replace a specific value in a text block when matching a key pattern ? I got the keys and the values from a command similar to: echo -e "key01 Nvalue01-1 Nvalue01-2 Nvalue01-3\nkey02 Nvalue02-1 Nvalue02-2 Nvalue02-3 \nkey03 Nvalue03-1... (2 Replies)
Discussion started by: alex2005
2 Replies

2. Shell Programming and Scripting

awk to remove mutiple values from specific pattern, leaving a single value

In the awk below I am trying to remove all instances after a ; (semi-colon) or , (comma) in the ANN= pattern. I am using gsub to substitute an empty string in these, so that ANN= is a single value (with only one value in it the one right after the ANN=). Thank you :). I have comented my awk and... (11 Replies)
Discussion started by: cmccabe
11 Replies

3. Shell Programming and Scripting

Pattern search and modify the values

I have one file and the file may contain 500 to 15,000 records. I need to search pattern ^F509= and then increment the corresponding value by one and print the entire line. Please note that Its not a fixed length file. Can anyone please help? ex: ^F509=204656 ^F509=204656 ... (6 Replies)
Discussion started by: vinus
6 Replies

4. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

5. UNIX for Dummies Questions & Answers

Sum up values followed by pattern

I have a file with data merged from multiple files. File contains header, data and trailer of multiple files. The trailer starts with 99 and delimiter is ~. Trailer 99~120 99~30 As it is a merged file we i have multiple lines followed by 99~. Need help to find sum of values which are there... (4 Replies)
Discussion started by: santoshdrkr
4 Replies

6. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

7. Shell Programming and Scripting

Adding Column Values Using Pattern Match

Hi All, I have a file with data as below: A,FILE1_MYFILE_20130309_1038,80,25.60 B,FILE1_MYFILE_20130309_1038,24290,18543.38 C,FILE1_dsc_dlk_MYFILE_20130309_1038,3,10.10 A,FILE2_MYFILE_20130310_1039,85,110.10 B,FILE2_MYFILE_20130310_1039,10,12.10... (10 Replies)
Discussion started by: angshuman
10 Replies

8. Shell Programming and Scripting

Compare values for a pattern match and execute script

Here in the input file 23:59:13,devicename,21,server1,700 23:59:13,devicename,22,server2,200 23:59:13,devicename,23,server3,200 23:59:13,devicename,24,server4,200 23:59:13,devicename,25,server5,200 23:59:13,devicename,26,server6,200 23:59:13,devicename,27,server7,200... (6 Replies)
Discussion started by: necro98
6 Replies

9. AIX

Pattern count for numeric values in aix

Hi All , I have a small code that checks pattern of digits entered in unix mode . $ echo 201202 | wc -c 7 /* output*/ When i run same command in AIX 5.1 , i am getting output with some initial blanks $ echo 201202 | wc -c 7 ... (4 Replies)
Discussion started by: Perlbaby
4 Replies

10. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies
Login or Register to Ask a Question