I have file with unconstatnt delimiter for each field which are non-printable characters like tab and space
file
6271 manchester (tab) 11/09/09 200 accepted
6272 manchester (tab) 11/09/09 200 accepted
I want only first 3 fields as it were with same delimiters
can any one help me out
thanks
$ awk '{print $1,$2,$3}' infile
This will give the same tab and space
$ nawk '{print substr($0,0,index($0,"/")+2)}' test
6271 manchester (tab) 11/09
6272 manchester (tab) 11/09
suppose I dont have any / in my 3 field
or
I want first 4 fields from file
thanks
$ perl -lane '$a=$_; $a =~ s/(.*\d\d\s).*([a-z])/$1/; print $a;' test
6271 manchester (tab) 11/09/09 200
6272 manchester (tab) 11/09/09 200
can we acheive using sed or awk
because I dont perl
thanks
$ nawk '{print substr($0,1,index($0,$4)+length($4))}' test
6271 manchester (tab) 11/09/09
6272 manchester (tab) 11/09/09
---------- Post updated at 04:11 PM ---------- Previous update was at 04:09 PM ----------
$ nawk '{print substr($0,1,index($0,$5)+length($5))}' test
6271 manchester (tab) 11/09/09 200
6272 manchester (tab) 11/09/09 200
With GNU awk 4 you can easily reconstruct the original variable field separator(s):
split(string, array [, fieldsep [, seps ] ])
Divide string into pieces separated by fieldsep and store the pieces in array and
the separator strings in the seps array.
For example:
awk '{
split($0, t, FS, s)
for (i = 1; ++i <= l;)
printf "%s", (t (i < l ? s : RS))
}' l=4 infile
% awk '{
split($0, t, FS, s)
for (i = 0; ++i <= l;)
printf "%s", (t (i < l ? s : RS))
}' l=5 infile
6271 manchester (tab) 11/09/09 200
6272 manchester (tab) 11/09/09 200
% awk '{
split($0, t, FS, s)
for (i = 0; ++i <= l;)
printf "%s", (t (i < l ? s : RS))
}' l=4 infile
6271 manchester (tab) 11/09/09
6272 manchester (tab) 11/09/09
% awk '{
split($0, t, FS, s)
for (i = 0; ++i <= l;)
printf "%s", (t (i < l ? s : RS))
}' l=2 infile
6271 manchester
6272 manchester