1832872 Members
2204 Online
110048 Solutions
New Discussion

cut content in a file

 
SOLVED
Go to solution
peterchu
Super Advisor

cut content in a file

I have a file called "abc.txt"
# cat abc.txt

79 10 1 "S036593" "SAND_A01" "sa110c" 02/11/04 0 0
6 1 2 "S036553" "VIOL_A01" "V0470C" 02/26/04 0 0



how can I cut the date column out to a file ?
I want to the result like that:
02/11/04
02/26/04


thx in advance.
8 REPLIES 8
curt larson_1
Honored Contributor
Solution

Re: cut content in a file

the easiest is probably

cat abc.txt | awk '{print $7;}'
curt larson_1
Honored Contributor

Re: cut content in a file

you could also do

cat abc.txt | cut -f7

cat abc.txt | sed 's|.*\(../../..\).*|\1|'
Hein van den Heuvel
Honored Contributor

Re: cut content in a file

Most importantly YOU have to decide what the date column looks like. Is it, as per Curt first solution the 7th space-seperated chunk of characters? Better be sure the input never has a surprise space for example:
6 1 2 "S03 553" "VIOL_A01" "V0470C" 02/26/04 0 0

Is it, as per Curt's second solution two chars, a slash, two chars, an other slash and two more chars? That rule will merrily trigger on: "blah bl/ah/ blah"
returning "bl/ah/ b"

Or do you need a tight rule like the following regulare expression in perl:
perl -p -e '$_="$1\n" if (/ (\d\d\/\d\d\/\d\d) /)' < abc.txt.

That looks for a space, two digits, a slash (escaped), two more digits, a slash, two more digits and a space. If it sees that, remember (the parentheses) the part between the spaces in $1 and copy to the default output string ($_) terminated by a \n.
'course that rule will not fire on 14/3/04 whether you consider that a valid date or not.

hth,
Hein.





Sanjay Kumar Suri
Honored Contributor

Re: cut content in a file

It is simple with cut:

cut -d " " -f7 abc.txt > out_file

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
T G Manikandan
Honored Contributor

Re: cut content in a file

$awk '{print $7}' /tmp/a >/tmp/b
Sanjiv Sharma_1
Honored Contributor

Re: cut content in a file

cat abc.txt | awk '{print $7;}'
Everything is possible
Victor Fridyev
Honored Contributor

Re: cut content in a file

Hi,

If you can be sure that the date is always in the 7th field, use
awk '{print $7}' abc.txt
But the following seems better:
awk '{print $(NF-2)}' abc.txt
Entities are not to be multiplied beyond necessity - RTFM
Keith Bevan_1
Trusted Contributor

Re: cut content in a file

Hi,

You could try this though its a little long winded and involves creating a temp file to disk.

sed/ /~/g abc.txt > abc.tmp
date=`pg abc.tmp | cut -f 7 -d "~"`

awk may be a better option as previously posted!

Keith
You are either part of the solution or part of the problem