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
# 8  
Old 07-19-2007
Quote:
Originally Posted by doer
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|
if you take a a closer look at the previous suggestions, you'll see that there's no assumptions of the 'number of underscores/dashes in the file. The only assumption (based on your sample file] is that you want to get the 'next to last' field in the underscoreORdash separated record/line.

Is the above correct description of the objective?
# 9  
Old 07-19-2007
Quote:
Originally Posted by divz
can you please explain hoe does ths work??? Smilie

Code:
sed 's/.*[-_]\([^-_][^-_]*\)[-_].*/\1/' myFile

from left to right:

.* - any character repeated 0 or more times - greedy - will consume ALL the character leading to the LAST non-underscore/non-dash char followed b dashORunderscore.
[-_] - followed by either a '-' or a '_' char
\([^-_][^-_]*\) - followed by a 'capture' of any character other then '-' or '_' repeater 0 or more times.
[-_] - followed by either a '-' or a '_' char
.* - any character repeated 0 or more times - greedy
\1 - replace the 'matched' string with the FIRST 'capture'

I know it might be a bit confusing reading the regEx expressions at times, but try to think 'pattern matching'....
# 10  
Old 07-19-2007
yes your objective is absolutely correct but how do I use this command

sed 's/.*[-_]\([^-_][^-_]*\)[-_].*/\1/' myFile in my below script.


Myscript

for DATA in `cat $IN_FILE/a.txt`
do
BSC_ID=`echo $DATA | awk -F[_-] '{print $(NF-1)}`
echo $BSC_ID
done

and the output is

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

which is incorrect.
# 11  
Old 07-19-2007
Smilie Thanks a lot for the explaination....
This is really good work.
# 12  
Old 07-19-2007
Quote:
Originally Posted by doer
yes your objective is absolutely correct but how do I use this command

sed 's/.*[-_]\([^-_][^-_]*\)[-_].*/\1/' myFile in my below script.


Myscript

for DATA in `cat $IN_FILE/a.txt`
do
BSC_ID=`echo $DATA | awk -F[_-] '{print $(NF-1)}`
echo $BSC_ID
done

and the output is

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

which is incorrect.
.... the same way you've used the 'awk' suggestion - although you've missed a single-quote from the original suggestion.

Here's the modified 'awk' way with optimized non-UUOC code - what the purpose of the 'for' loop?:
Code:
awk -F[_-] '{print $(NF-1)}' $IN_FILE/a.txt

The same results can be achieved with the similar 'sed' solution no need for the 'for' loop either:
Code:
sed 's/.*[-_]\([^-_][^-_]*\)[-_].*/\1/' $IN_FILE/a.txt


Last edited by vgersh99; 07-19-2007 at 01:58 AM..
# 13  
Old 07-19-2007
cat filename | sed 's/-/_/' | awk -F"_" '{print $5}'
# 14  
Old 07-19-2007
Quote:
Originally Posted by aajan
cat filename | sed 's/-/_/' | awk -F"_" '{print $5}'
cat and sed and awk...... why?
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