Third Last Character from AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Third Last Character from AWK
# 1  
Old 04-28-2011
Third Last Character from AWK

How to get the third last character for a delimited input string using awk?

For instance if input string is "a/b/c/d/e/f/s/w/q/t/g/y" (delimited by “/”) then output should be “t”
Similarly for string “"a/b/c/d/e/f/s/w", the output should be “w”.

The length of string is uncertain, so we cannot go for cut command.

With SED it's working echo "a/b/c/d/e/f/s/w/q/t/g/y" | sed -e 's|/[a-z]/[a-z]$||' -e 's|[a-z]||' -e 's|/.*/||' I want this with AWK....
# 2  
Old 04-28-2011
Code:
echo "a/b/c/d/e/f/s/w/q/t/g/y" |awk -F \/ '{print $(NF-2)}'

Quote:
Originally Posted by Shell_Learner
[FONT=Arial]
Similarly for string “"a/b/c/d/e/f/s/w", the output should be “w”.
w or f ????
# 3  
Old 04-28-2011
F..


but it is giving error....
# 4  
Old 04-28-2011
Quote:
Originally Posted by Shell_Learner
F..


but it is giving error....
What kind of error?
# 5  
Old 04-28-2011
=> echo "a/b/c/d/e/f/s/w/q/t/g/y" |awk -F \/ '{print $(NF-2)}'
awk: syntax error near line 1
awk: bailing out near line 1
# 6  
Old 04-28-2011
Quote:
Originally Posted by Shell_Learner
=> echo "a/b/c/d/e/f/s/w/q/t/g/y" |awk -F \/ '{print $(NF-2)}'
awk: syntax error near line 1
awk: bailing out near line 1
Use nawk or /usr/xpg4/bin/awk on Solaris.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove character \r and \n in awk

Hi Everybody: I need your help, please... I have this file *.txt 0000 | 16010201 22000000 67892000 00000000 00000000 00000100 72246681 28E08236 | ~~~~"~~~g~ ~~~~~~~~~~~~~r$f~(~~6 | 0020 | 10476173 90010100 10000000 00000001 05000226 17163011 12442212 48140484 |... (2 Replies)
Discussion started by: solaris21
2 Replies

2. Shell Programming and Scripting

How to delete a character using sed and or awk?

Hi, 1/ i have file test.txt 1 Jul 28 08:35:29 2014-07-28 Root::UserA 1 Jul 28 08:36:44 2014-07-28 Root::UserB i want to delete the seconds of the file, and the Root:: and the output will be: 1 Jul 28 08:35 2014-07-28 UserA 1 Jul 28 08:36 2014-07-28 UserB 2/i have another file test2.txt:... (8 Replies)
Discussion started by: fxsme
8 Replies

3. Shell Programming and Scripting

awk trailing character removal

Hi, The command - id | awk '{print $1}' - returns the following: uid=9028(luke) What do I need to further that awk so that I only have "luke", I want to set this as a variable. Thanks in advance, Lukas. P.S: I've come up with: USER1=`id | awk F'(' '{print $2}' | awk -F')' '{print... (4 Replies)
Discussion started by: luke222010
4 Replies

4. Shell Programming and Scripting

Want to remove / and character using awk or sed

Below i am trying to remove "/" and "r" from the output, so i need output as: hdiskpower3 hdisk0 hdisk1 #inq | grep 5773 | awk '{print $1}' | sed 's/dev//g' | awk -F"/" '{$1=$1}1' .....................................................//rhdiskpower0 //rhdiskpower1 //rhdiskpower2... (3 Replies)
Discussion started by: aix_admin_007
3 Replies

5. Shell Programming and Scripting

Character printing with AWK

I was wondering if I could use AWK to print from the nth character position to nth for all records in the file. All I have been doing is printing/copying fields rather than characters. I found a way to print lines greater/less/equal to a character length which led me to believe there is a way... (3 Replies)
Discussion started by: MIA651
3 Replies

6. UNIX for Dummies Questions & Answers

character limit via awk

I'm using a command that outputs the total size of the files that I've specified. I'd like to introduce a character limit that appends an ellipsis to the lines that go beyond the specified amount. du -chs {query} | sed 's!.*/!!' | awk '{print substr($0,0,25)""}' That's what I have so far.... (4 Replies)
Discussion started by: Light_
4 Replies

7. UNIX for Dummies Questions & Answers

Use of character range in awk

Hi all, I am having a bit of a hard time using awk. I must do something wrong, but I don't know what... Any help would be greatly appreciated! I read a file, as follows :... ATOM 21 C THR A 4 23.721 -26.194 1.909 1.00 32.07 C ATOM 22 O THR A 4 ... (2 Replies)
Discussion started by: hypsis
2 Replies

8. Shell Programming and Scripting

awk escape character

ll|awk '{print "INSERT INTO SCHEMA.TABLE_NAME VALUES (`"$9 "`,"$5");" }' INSERT INTO SCHEMA.TABLE_NAME VALUES (``,); INSERT INTO SCHEMA.TABLE_NAME VALUES (`TABLE_PARTITION_Y2010M03D06.dmp`,7923328); INSERT INTO SCHEMA.TABLE_NAME VALUES (`TABLE_PARTITION_Y2010M03D06.log`,1389); But I want ' in... (2 Replies)
Discussion started by: faruque.ahmed
2 Replies

9. Shell Programming and Scripting

Awk to cut character

Hi All, I have txt file : SubNetwork=ONRM_ROOT_MO,SubNetwork=RNC2,MeContext=3141_RBI_Mumong_Exchange,ManagedElement=1,SystemFunctions=1,Licensing=1 then can we use awk command to get result : ONRM_ROOT_MO_RNC2,3141_RBI_Mumong_Exchange Thanks, Bow (9 Replies)
Discussion started by: justbow
9 Replies

10. Shell Programming and Scripting

awk / escape character

Hi I'm trying to split a dir listing eg /home/foo1/foo2 I'm using ksh I've tried dir=/home/foo1/foo2 splitit=`echo $dir | awk -F '\/' '{print $1}'` echo $splitit nothing is output! I have checked the escape character. The only one I have found is \ BTW `pwd` | awk -F \/... (8 Replies)
Discussion started by: OFFSIHR
8 Replies
Login or Register to Ask a Question