[SGVLUG] Shell fun: cutting the last field of a record...

Robert mrflash818 at geophile.net
Fri Mar 24 21:01:19 PST 2006


To remove the last trailing character (brute force) how about this:

cat $REC | sed 's/.$//'

Basically it brute force removes the last character on a line, so you
would only want to do it on the last line where your ctrl-M is showing up.

...just my quick stab at a solution idea for you.





> I've got a file delimited with vertical-bar characters (|)
>
> The last field of each record is a filename, and there is no trailing
> "|" in the record.
>
> I've got a process that is extracting the name as follows:
>
>    for REC in `cat worklist`
>    do
>       fname=`echo $REC | cut -f6 -d"|"`
>       <does something thaqt creates the file "$fname">
>    done
>
> Say for instance the file "worklist" looks like this:
>
>    rec1|field2|field3|field4|field5|filea
>    rec2|field2|field3|field4|field5|fileb
>    rec3|field2|field3|field4|field5|filec^M  (file has an embedded
> carraige return at the end)
>
> When I'm done, a listing of the directory with "-b" (show embedded
> specials as \octal) shows that the carraige return ended up as part of
> the filename:
>
>    filea
>    fileb
>    filec\015
>
> how can I eliminate that trailing "/r" ?  (and before you ask, the
> workfile is being generated by a database extract of fields that were
> populated by a [visual] spreadsheet-style database query tool, so
> inadvertantly pressing <enter> during data entry could easily go
> unnoticed)
>
>


-- 
"Knowledge is Power" -- Francis Bacon

Robert Leyva
mrflash818 at geophile.net




More information about the SGVLUG mailing list