LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-31-2005, 08:35 AM   #1
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Rep: Reputation: 32
Regex Help


Hi all,
I have a file I an trying parse that looks like so:
Code:
Param1        " 12"       '48H 
Param2        "150"       '4AH 
Param3        " 10"       '4CH.
UNUSED        " -1"
UNUSED        " -1"
UNUSED        " -1"
The values in quotes are 3 characters long. -1 represents an unused field.

I want to replace all used field values with a dummy sential value, say 111.
So the file will now look like this:
Code:
Param1        "111"       '48H 
Param2        "111"       '4AH 
Param3        "111"       '4CH.
UNUSED        " -1"
UNUSED        " -1"
UNUSED        " -1"
all I really need is something like:
[code]$> cat filename|sed "s/(SomeRegex)/(SomeOtherRegex)/"

I'm having trouble cooking up the right regex.

Any thoughts?

Chris
 
Old 03-31-2005, 08:51 AM   #2
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Original Poster
Rep: Reputation: 32
If at first you don't succeed...
Code:
$> cat filename|sed "s/\" *[0-9]*\"/\"111\"/"
 
Old 03-31-2005, 09:14 AM   #3
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
Code:
SF1B : /supmis/soumen/tmp > cat ifile
Param1        " 12"       '48H
Param2        "150"       '4AH
Param3        " 10"       '4CH.
UNUSED        " -1"
UNUSED        " -1"
UNUSED        " -1"
SF1B : /supmis/soumen/tmp > awk -F\" 'BEGIN {OFS="\""} $2 == -1 {$2=111} {print}' ifile
Param1        " 12"       '48H
Param2        "150"       '4AH
Param3        " 10"       '4CH.
UNUSED        "111"
UNUSED        "111"
UNUSED        "111"
SF1B : /supmis/soumen/tmp >
HTH.
 
Old 03-31-2005, 09:36 AM   #4
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Original Poster
Rep: Reputation: 32
That might be a useful reference, but it does precisely the opposite of what I want it to do.

Thanks anyway for the effort.
 
Old 03-31-2005, 11:01 AM   #5
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
My bad... Anyway, here goes...

Code:
SF1B : /supmis/soumen/tmp > awk -F\" 'BEGIN {OFS="\""} $2 != -1 {$2=111} {print}' ifile
Param1        "111"       '48H
Param2        "111"       '4AH
Param3        "111"       '4CH.
UNUSED        " -1"
UNUSED        " -1"
UNUSED        " -1"
SF1B : /supmis/soumen/tmp >
 
Old 03-31-2005, 10:13 PM   #6
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30
I did it in perl just for fun. I'll take any excuse to use lookaheads!

here's the one-liner...

Code:
perl -e "while (<>) { s/(?<=\")\s*\d+(?=\")/111/; print }" filename
Or in a file...

Code:
#!/usr/bin/perl
while (<>) { s/(?<=")\s*\d+(?=")/111/; print }
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regex Perl help igotlongestname Programming 2 09-14-2005 07:51 PM
regex in paths basher400 Linux - Newbie 3 06-15-2005 07:08 AM
Help with Sed and regex cmfarley19 Programming 6 11-18-2004 01:09 PM
GNU C++ Regex lumux Programming 5 09-29-2003 10:51 PM
ip address REGEX Robert0380 Programming 16 08-15-2003 01:00 PM


All times are GMT -5. The time now is 07:22 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration