unix - Awk formatting by field separators -
to start prefer not using sed remove last part; reason because file has lot of variables have with.
echo $new
it display
jack: grow far away. loves food hotdogs, hamburgers, , steak. has lot more text. metro filer:
so it's 1 lone line of text need break, rid of metro filer:
generically possible can use other variables (100+ other variables).
also, says it has lot more text
means there lot more text there, don't want make massive string. there colon @ beginning , end.
what is
jack: grow far away. loves food hotdogs, hamburgers, , steak. has lot more text.
i tried couple things awk -f ":"
, couldn't seem include jack:
, great!
awk
$ echo "$new" | awk '{a=$1;for(i=2;i<nf-1;++i)a=a fs $i;print a}' jack: grow far away. loves food hotdogs, hamburgers, , steak. has lot more text.
it assigns first field a
, appends space , next field last 2 fields, , prints variable.
more readable:
{ string = $1 (i = 2; < nf-1; ++i) string = string fs $i print string }
rev
, cut
$ echo "$new" | rev | cut -d ' ' -f 3- | rev jack: grow far away. loves food hotdogs, hamburgers, , steak. has lot more text.
this reverses output, extracts space separated fields starting third, , reverses again.
parameter expansion
$ echo "${new% * *}" jack: grow far away. loves food hotdogs, hamburgers, , steak. has lot more text.
this says: remove end "space whatever space whatever", in non-greedy fashion.
sed
this isn't more complicated respect having many variables of other methods.
$ echo "$new" | sed 's/ [^ ]* [^ ]*$//' jack: grow far away. loves food hotdogs, hamburgers, , steak. has lot more text.
this removes space , 2 non-space groups, separated space, end of string.
Comments
Post a Comment