How reverse cut or read rows of lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How reverse cut or read rows of lines
# 1  
Old 07-18-2007
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 using the command
echo $DATA | awk -F_ '{print $(NF-1)}'
I get LIMJM1-3, 4, 11.

I want to extract 3,4, and 11 only.

please help.
# 2  
Old 07-18-2007
Code:
sed 's/.*[-_]\([^-_][^-_]*\)[-_].*/\1/' myFile

# 3  
Old 07-19-2007
Quote:
Originally Posted by doer
echo $DATA | awk -F_ '{print $(NF-1)}'
try this:
Code:
echo $DATA | awk -F[_-] '{print $(NF-1)}'

# 4  
Old 07-19-2007
when i use the BSC_ID=`echo $DATA | awk -F[_-] '{print $(NF-1)}`

i get
BSC403_JAIN03
BSC403_JAIN03|3153_TropicalFarm_LIMJM1-3_97|
BSC403_JAIN03
BSC403_JAIN03|3410_PantaiAceh_PCEHM1_4_97|
BSC406_BMIN02
BSC406_BMIN02|1433_JomHebohTV3_COW7M1_11_97|

which is incorrect
# 5  
Old 07-19-2007
to more precise the number of underscores are not fixed in my file.

BSC403_JAIN03|3153_TropicalFarm_LIMJM1-3_97|
BSC403_JAIN03|3410_PantaiAcehPCEHM1_4_97|
BSC406_BMIN02|1433_JomHebohTV3_COW7M1_11_97|

so that is the reason why I want to read from reverse and get the value before _97|
# 6  
Old 07-19-2007
Quote:
Originally Posted by vgersh99
Code:
sed 's/.*[-_]\([^-_][^-_]*\)[-_].*/\1/' myFile

can you please explain hoe does ths work??? Smilie
# 7  
Old 07-19-2007
Quote:
Originally Posted by doer
when i use the BSC_ID=`echo $DATA | awk -F[_-] '{print $(NF-1)}`

i get
BSC403_JAIN03
BSC403_JAIN03|3153_TropicalFarm_LIMJM1-3_97|
BSC403_JAIN03
BSC403_JAIN03|3410_PantaiAceh_PCEHM1_4_97|
BSC406_BMIN02
BSC406_BMIN02|1433_JomHebohTV3_COW7M1_11_97|

which is incorrect
Firstly, your command is what what it's been originally suggested by ghostdog74 (which does work for your sample input):
Code:
echo $DATA | awk -F[_-] '{print $(NF-1)}'

Secondly, have you tried the 'sed' suggestion yet?
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

Reverse even lines

I'm trying to reverse every even line in my file using the awk command below but it prints only the odd lines but nothing else: $ awk '(NR % 2) {print}; !(NR % 2) {print | "rev";}' myfile Any idea what I might have done wrong? Thank you. (10 Replies)
Discussion started by: ivpz
10 Replies

3. Shell Programming and Scripting

How to cut the particular string and increment the rows?

Hi am using unix aix I have tried using awk but am getting only output = x ,its not incrementing next output set -A var1 vv qa za ct=0 i=3 while do var1=`echo ${var1}` count=`awk ' NR==$i++ {print;exit}' ${.txt} | cut -c5 ` echo $count let ct=ct+1 done (6 Replies)
Discussion started by: Venkatesh1
6 Replies

4. Shell Programming and Scripting

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 PackageName-U939393-8.2.3.4.s390x.rpm But the s390x could be any string w/o periods in it, x8664 for example,... (9 Replies)
Discussion started by: cbo0485
9 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

Print rows in reverse order if values decrease along the column

Hi, Guys. Please help me to find solution to this problem using shell scripting. I have an INPUT file with 4 columns separated by tab. Each block of records is separated by ----- ----- Sample1 5402 6680 Pattern01 Sample2 2216 2368 Pattern02... (6 Replies)
Discussion started by: sam_2921
6 Replies

7. 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

8. Shell Programming and Scripting

read line in reverse order from file

dear all i want to read 5th no of line from last line of file. kindly suggest me possible ways. rgds jaydeep (2 Replies)
Discussion started by: jaydeep_sadaria
2 Replies

9. HP-UX

Cut rows

Hi, I have a requirement to search for two words and grep all the lines between them. For e.g. : $cat file.dat abc,To,number acd,To,cnz \* flexibile select *\ bcd,To,lla anz,From,kln app,From,lpz I need to get all the lines between the lines cantaining word 'acd' and 'anz'. the... (2 Replies)
Discussion started by: obedkhan
2 Replies

10. Shell Programming and Scripting

Need to read a file in reverse

I have to extract data from a text file which is huge in size >>10GB. ie between two strings. If I do an ordinary sed it takes forever to come out. I was wondering if there was anyway to do the entire process in reverse and on finding the relevant string is there any way to break out of the... (5 Replies)
Discussion started by: scorreg
5 Replies
Login or Register to Ask a Question