Cut or awk in reverse


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut or awk in reverse
# 1  
Old 05-11-2011
Cut or awk in reverse

I may be making this too hard on myself, but I'm trying to find a way that I can use a cut or awk string to always remove the last two delimited fields of a string.


Say I have

Code:
PackageName-U939393-8.2.3.4.s390x.rpm

But the s390x could be any string w/o periods in it, x8664 for example, etc... I need to use cut or awk to remove the .s390x.rpm piece.

I was using '.' as the delimiter, but the number of '.' will vary.
# 2  
Old 05-11-2011
Code:
$ echo 'PackageName-U939393-8.2.3.4.s390x.rpm' | awk -F. '{NF-=2;}1' OFS='.'
PackageName-U939393-8.2.3.4

NF is an internal variable that specifies Number of Fields. You can then access the last field as $NF, second to last as $(NF-1) etc.
Here I just decrement NF by two and set the OFS (output field separator) to '.'. The '1' is a shortcut for 'print $0'.
# 3  
Old 05-11-2011
Could it be a sed based solution? Maybe sed suits better this kind of problem. This regular expression:

Code:
\.[^.]*\.[^.]*$

means: the last two dot delimited fields. So, we just need to use this regex into a sed substitution command:

Code:
$ echo PackageName-U939393-8.2.3.4.s390x.rpm | sed 's/\.[^.]*\.[^.]*$//'
PackageName-U939393-8.2.3.4
$ echo PackageName-U939393-8.2.3.4.x8664.rpm | sed 's/\.[^.]*\.[^.]*$//'
PackageName-U939393-8.2.3.4

Of course, awk and cut can be used too, as mirni showed us, but I think sed is simpler.
# 4  
Old 05-11-2011
Code:
$ str=PackageName-U939393-8.2.3.4.s390x.rpm
$ echo "${str%.*.*}"
PackageName-U939393-8.2.3.4

# 5  
Old 05-11-2011
Quote:
Originally Posted by Scrutinizer
Code:
$ str=PackageName-U939393-8.2.3.4.s390x.rpm
$ echo "${str%.*.*}"
PackageName-U939393-8.2.3.4

This one is by far the fastest solution as it is based only on the shell internals Smilie
# 6  
Old 05-12-2011
Quote:
Originally Posted by mirni
Code:
$ echo 'PackageName-U939393-8.2.3.4.s390x.rpm' | awk -F. '{NF-=2;}1' OFS='.'
PackageName-U939393-8.2.3.4

NF is an internal variable that specifies Number of Fields. You can then access the last field as $NF, second to last as $(NF-1) etc.
Here I just decrement NF by two and set the OFS (output field separator) to '.'. The '1' is a shortcut for 'print $0'.
Just for my personnal comprehension ... got the 1 part but not the ; in the {NF-=2;} Can somebody expain? Cause it works without it.
# 7  
Old 05-12-2011
The semicolon is superfluous there, one could just leave it out, or even do this:
Code:
echo 'PackageName-U939393-8.2.3.4.s390x.rpm' | awk -F. NF-=2 OFS=.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to cut part of a string in reverse?

Hi, how to cut part of a string sing delimiter in reverse input file 1,2,st-pa-tr-01,2,3,4, 2,3,ff-ht-05,6,7,8 how can i obtain strings till st-pa-tr ff-ht i.e cutting the last part og string -01 and -05 Thanks & Regards Nivi edit by bakunin: changed thread title (typo) (3 Replies)
Discussion started by: nivI
3 Replies

2. Shell Programming and Scripting

awk reverse string

Hello, Can anyone explain for me in this script to reverse the string? 1) the "x=x" part, how it works? $ echo welcome | awk '{ for(i=length;i!=0;i--)x=x substr($0,i,1);}END{print x}' $ emoclew2) x seems to be an array at the END, but can it automatically print the whole array in awk? Thanks... (8 Replies)
Discussion started by: yifangt
8 Replies

3. Shell Programming and Scripting

Help with sed/awk for reverse search and print

I have a file which is DFDG START DSFDS DSDS XXX END (VIO) AADD START SDSD FGFG END and I have to print the lines between START and END (VIO). In the files there are multiple places where START would be followed by END with few lines in between but I need to print only if START is... (18 Replies)
Discussion started by: pgbuddy
18 Replies

4. Shell Programming and Scripting

Reverse script usage -Awk

Is it possible to convert EXONtoBED (Output to input)? 1. BEDtoExon.sh ------------------- awk '{ n11 = split($11, t11, ",") n12 = split($12, t12, ",") for (i = 0; ++i < n11;) { s12 = $2 + t12 print $1, s12, s12 + t11, i”E_”$4 } }' $1 ... (1 Reply)
Discussion started by: quincyjones
1 Replies

5. Shell Programming and Scripting

CUT command delimiter in reverse

Hi, I've a situation where, a=xxx.yyy.zzz.txt EXTN=`echo $a | cut -d . -f2` Using the above code it delimites and will return "yyy.zzz.txt" to EXTN. But i need to get only the extension "txt". so as per the above code it delimits in the first "." itself. Can anyone help how to do... (6 Replies)
Discussion started by: skcvasanth
6 Replies

6. Shell Programming and Scripting

cut a field, but with reverse order

Hi Everyone, I have one a.txt: a b 001 c b b 002 c c c, not 002 c The output should be 001 002 002 If i use cut -f 3 -d' ', this does not work on the 3rd line, so i thought is any way to cut the field counting from the end? or any perl thing can do this?:confused: ... (3 Replies)
Discussion started by: jimmy_y
3 Replies

7. Shell Programming and Scripting

awk, shell script reverse engineering app generator - project

Hi, this is fantastic forum for shell programming and scripting, so please let me to introduce you with my very old concept to have web form/s with radio, select, input fields and have an application generating valid, syntax error free scripting code. The same or alike questions are asked... (2 Replies)
Discussion started by: darius2
2 Replies

8. Shell Programming and Scripting

reverse search in awk script

Hi, I am new to awk. Actually I want to search a pattern A, when I get that line with pattern A then for one of the field of that line again I want search on that field (say pattern B)from start of the file. I am using awk. Is nested searching possible in awk? Please do the needful as... (4 Replies)
Discussion started by: id4forum
4 Replies

9. Shell Programming and Scripting

[grep awk cut] > awk

Hi, I'm very new to scripting. grep $s $filename | awk '{print $2}' | cut -c 1-8 How can I optimize this using a single awk? I tried: awk '/$s/ {print $2}' $filename | cut -c 1-8 However didn't work, I think the awk is not recognizing $s and the verbal is something else. (6 Replies)
Discussion started by: firdousamir
6 Replies

10. Shell Programming and Scripting

How reverse cut or read rows of lines

Hi, My records are like this BSC403_JAIN03|3153_TropicalFarm_LIMJM1-3_97| BSC403_JAIN03|3410_PantaiAceh_PCEHM1_4_97| BSC406_BMIN02|1433_JomHebohTV3_COW7M1_11_97| I want to extract the value before _97| This command BSC_ID=`echo $DATA | cut -f5 -d"_"` gives me _97|, 4, 11 and by... (16 Replies)
Discussion started by: doer
16 Replies
Login or Register to Ask a Question