General
cancel
Showing results for 
Search instead for 
Did you mean: 

Script for capturing only few values from the first line of a file

SOLVED
Go to solution
Anant
Occasional Contributor

Script for capturing only few values from the first line of a file

Hello,
I understand that this is not a scripting forum, but I believe this should be very easy for you experts.

First line of my file contains some data like this "abcdefg.xx 123 xx.yy.zz.11 There is some variable length text here 03/10/09"
Note that:
1) where I see "123" it will never be more than 4 characters. But it can be of zero characters too. Meaning it does not exist.
2) Line will always end with a date, so it will contain a character "/" in it.
3) What I am want to extract is the value "xx.yy.zz.11 There is some variable length text here". This could be of any length, any number of words, any characters.

Do any of you have a script/command to make this happen? Lets assume that filename is workfile.txt.
7 REPLIES
James R. Ferguson
Acclaimed Contributor
Solution

Re: Script for capturing only few values from the first line of a file

Hi:

# X="abcdefg.xx 123 xx.yy.zz.11 There is some variable length text here 03/10/09"

# Y="abcdefg.xx xx.yy.zz.11 There is some variable length text here 03/10/09"

# echo ${X}|perl -nle 'm{.+?\s+(\d+\s+)*(.+)} and print $2'
xx.yy.zz.11 There is some variable length text here 03/10/09

...and:

# echo ${Y}|perl -nle 'm{.+?\s+(\d+\s+)*(.+)} and print $2'
xx.yy.zz.11 There is some variable length text here 03/10/09

...which distinguishes rule-1.

Regards!

...JRF...
George Spencer_4
Frequent Advisor

Re: Script for capturing only few values from the first line of a file

How about:

perl -ne 'print "$1\n" if /^[A-Za-z]+\.[0-9][0-9].* ([0-9][0-9]\.[0-9][0-9]\.[0-
9][0-9]\.11) .+ [0-1]?[0-9]\/[0-3]?[0-9]\/[0-9][0-9]$/' workfile.txt
Dennis Handly
Acclaimed Contributor

Re: Script for capturing only few values from the first line of a file

>Do any of you have a script/command to make this happen?

You could use awk:
awk '
{
if (length($2) > 4) # optional field
start = 2
else
start = 3 # skip optional field
printf "%s", $start
for (i = start+1; i < NF; ++i)
printf " %s", $i
print ""
exit
}' workfile.txt.
Roland Piette
Regular Advisor

Re: Script for capturing only few values from the first line of a file

Hello Anant,

Try this also :

head -1 workfile.txt | sed 's/^.*123 \(.*$\)/\1/'

If "123" is not present in the line, the commands will give the entire line

If you need nothing when "123" is not present, insert before sed a grep command so you will have this :

head -1 workfile.txt | grep 123 | sed 's/^.*123 \(.*$\)/\1/'

Good luck,
Roland
David DiBiase
Frequent Advisor

Re: Script for capturing only few values from the first line of a file

maybe simpler

Assuming a file named 'file' contains:
Please wait...checking for disk quotas
(c)Copyright 1983-2000 Hewlett-Packard Co., All Rights Reserved.

$ line $ echo $first
Please
$ echo $second
wait...checking for disk quotas
$ line $ echo $first
Please
$ echo $second
wait...checking
$ echo $rest_of_line
for disk quotas
$
Bob E Campbell
Honored Contributor

Re: Script for capturing only few values from the first line of a file

Are we making this too complex? If you are only looking for column 2 with space delimters:

# cat foo
abcdefg.xx 123 xx.yy.zz.11 There is some variable length text here 03/10/09
abcdefg.xx 13 xx.yy.zz.11 There is some variable length text here 03/10/09
abcdefg.xx xx.yy.zz.11 There is some variable length text here 03/10/09
abcdefg.xx 3 xx.yy.zz.11 There is some variable length text here 03/10/09
# cut -d\ -f2 foo
123
13

3
Anant
Occasional Contributor

Re: Script for capturing only few values from the first line of a file

People responding to my questions, provided the necessary solutions. Thanks to all of them.