Computing.Net > Forums > Unix > Remove Last Word in File

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Remove Last Word in File

Reply to Message Icon

Name: KSHnewbie
Date: August 17, 2006 at 16:05:54 Pacific
OS: windows 2003
CPU/Ram: n/a
Product: n/a
Comment:

Hi,

I am trying to find out how to remove the last word in a file.

For example, a line in a file will contain:
one two three four

And I want to remove the word "four" from the line.

Thanks!




Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: August 17, 2006 at 22:40:54 Pacific
Reply:

sed '$s/ *\([^ ]* *\)$//' data.file


0

Response Number 2
Name: KSHnewbie
Date: August 18, 2006 at 05:29:59 Pacific
Reply:

Nails,

Can you please explain what sed does? I'm just unfamiliar with that command. Is there another way using ksh commands?


0

Response Number 3
Name: nails
Date: August 18, 2006 at 10:20:37 Pacific
Reply:

sed stands for Stream EDitor. It's a common utility used to edit text files from the command line or a script. This particular example prints all lines, but the last. On the last line it uses a regular expression to print everything, but the last field.

What is your definition of 'ksh commands?' You can't use any external commands. There is a way, but first I need the total number of lines in the file. I'd normally use:

flen=$(wc -l < data.file)

but this loop works:

flen=0
while read line
do
((flen+=1))
done < data.file

Now, use this loop:

cnt=0
while read line
do
((cnt+=1))
if [[ $cnt -eq $flen ]]
then # last line
# set the separate fields as command line arguments
set - $(echo "$line")
cnt2=1
while [[ $cnt2 < $# ]]
do
argchar=$(eval echo \$$cnt2)
printf "%s " $argchar
((cnt2+=1))
done
else
echo "$line"
fi
done < data.file
echo
# end script

In the above script, print everything, but the last line. On the last line set each field equal to the command line arguments $1, $2, etc.

The above script works as long as the last line doesn't have over 9 fields. If it does have over 9 fields, the solution is a little more drawn out.


0

Response Number 4
Name: ghostdog
Date: August 19, 2006 at 23:47:14 Pacific
Reply:

here's one that is more readable: in python though

>>> o = open("output.txt","a")
>>> yourfile = os.path.join("/home","file.txt")
>>> for lines in open(yourfile):
.......# get all words except the last word
.......line = lines.split()[:-1]
.......o.write(' '.join(line) + "\n")
...
>>> o.close()
>>>


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Remove Last Word in File

Shell script:find word in big file www.computing.net/answers/unix/shell-scriptfind-word-in-big-file/4483.html

To remove last occurance of character www.computing.net/answers/unix/to-remove-last-occurance-of-character/3646.html

Delete last character in a file www.computing.net/answers/unix/delete-last-character-in-a-file/5310.html