The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
read some lines from file!!! andy2000 Shell Programming and Scripting 7 03-28-2007 01:55 AM
Read lines from file sagolo UNIX for Dummies Questions & Answers 2 11-27-2006 12:14 AM
how to read lines one by one from a file bihani4u UNIX for Dummies Questions & Answers 5 09-28-2006 04:55 PM
Need to read a file in reverse scorreg Shell Programming and Scripting 5 03-01-2006 09:14 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 07-18-2007
Registered User
 

Join Date: Jul 2007
Posts: 10
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.
Reply With Quote
Forum Sponsor
  #2  
Old 07-18-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,003
Code:
sed 's/.*[-_]\([^-_][^-_]*\)[-_].*/\1/' myFile
Reply With Quote
  #3  
Old 07-18-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,580
Quote:
Originally Posted by doer View Post
echo $DATA | awk -F_ '{print $(NF-1)}'
try this:
Code:
echo $DATA | awk -F[_-] '{print $(NF-1)}'
Reply With Quote
  #4  
Old 07-18-2007
Registered User
 

Join Date: Jul 2007
Posts: 10
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
Reply With Quote
  #5  
Old 07-18-2007
Registered User
 

Join Date: Jul 2007
Posts: 10
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|
Reply With Quote
  #6  
Old 07-18-2007
Registered User
 

Join Date: Jul 2007
Posts: 21
Quote:
Originally Posted by vgersh99 View Post
Code:
sed 's/.*[-_]\([^-_][^-_]*\)[-_].*/\1/' myFile
can you please explain hoe does ths work???
Reply With Quote
  #7  
Old 07-18-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,003
Quote:
Originally Posted by doer View Post
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?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:43 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0