Need a command to remove the last word in the first line of a file


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Need a command to remove the last word in the first line of a file
# 1  
Old 08-22-2019
Need a command to remove the last word in the first line of a file

I have a eg file op.txt
Code:
This is a cat
This is a fat cat
This is a fat black cat


I want to remove only the word cat from the first alone .can somebody help.

Moderator's Comments:
Mod Comment please do wrap your samples in CODE TAGS
As per forum rules.

Last edited by RavinderSingh13; 08-22-2019 at 10:42 PM..
# 2  
Old 08-22-2019
Code:
sed -i '1{s/\w*\W*$//;}' op.txt


Last edited by rdrtx1; 02-18-2020 at 08:26 PM..
# 3  
Old 08-22-2019
Try also
Code:
awk 'NR == 1 {$NF = ""} 1' file
This is a 
This is a fat cat
This is a fat black cat

awk 'NR == 1 {sub (/cat$/, "")} 1' file
This is a 
This is a fat cat
This is a fat black cat

# 4  
Old 08-22-2019
am getting this error

Code:
awk: syntax error near line 1
awk: bailing out near line 1

--- Post updated at 07:02 PM ---

Code:
sed: illegal option -- i
error am receiveing

Moderator's Comments:
Mod Comment Please do wrap your samples in CODE TAGS as per forum rules.

Last edited by RavinderSingh13; 08-22-2019 at 10:43 PM..
# 5  
Old 08-22-2019
Code:
printf "1 s/\\\w*\\\W*$//\nw\nq\n" | ed - op.txt


Last edited by rdrtx1; 02-18-2020 at 08:26 PM..
# 6  
Old 08-22-2019
Might have been smart to mention your OS version...


Quote:
Originally Posted by Don Cragun
If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk or nawk instead of awk.
# 7  
Old 08-23-2019
Remove the first word in line 2
Code:
sed '2 s/^ *[^ ]\{1,\} *//' op.txt

Line 2, s(ubstitute) what the BRE ^ *[^ ]\{1,\} * matches with nothing.
BRE: at the beginning of the line any spaces then at least one non-space then any spaces.

Remove the last word in line 1
Code:
sed '1 s/ *[^ ]\{1,\} *$//' op.txt

Line 1, s(ubstitute) what the BRE *[^ ]\{1,\} *$ matches with nothing.
BRE: any spaces then at least one non-space then any spaces at the end of the line.

@rdrtx1, \w and \W and \s and \S are defined in the Perl RE, a newer glibc (GNU-Linux) is needed.

If portability mattters then stick to the original: perl.
Code:
perl -i -lpe '$.==1 and s/\s*\S+\s*$//' op.txt

The -i option writes the output back to the input file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - sed - Remove first word from line which can begin eventually with blank

hello. How to remove first word from line. The line may or may not start with blank. NEW_PARAM1=$(magic-command " -t --protocol=TCP -P 12345-u root -h localhost ") NEW_PARAM2=$(magic-command "-t --protocol=TCP -P 12345 -u root -h localhost ") I want NEW_PARAM1 equal to NEW_PARAM2 equal ... (2 Replies)
Discussion started by: jcdole
2 Replies

2. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

3. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

4. Shell Programming and Scripting

sed command to remove a word from string

Hello All, I am running a command find . -name amp.cfg | cut -c 3- which gives me output something like below rel/prod/amp.cfg rel/fld/amp.cfg deb/detail/amp.cfg deb/err/amp.cfg I want to remove trailing "/amp.cfg" so that i should get output something like... (7 Replies)
Discussion started by: anand.shah
7 Replies

5. Shell Programming and Scripting

How to remove all words from a matching word in a line?

Hi Guys, :p I have a file like this: 2010-04-25 00:00:30,095 INFO - ]- start process U100M4 2010-04-25 00:00:30,096 DEBUG - ] -- call EJB 2010-04-25 00:00:30,709 INFO - - end processU100M4 2010-04-25 00:00:30,710 DEBUG - got message=Sorry I want to out put format. 2010-04-25... (5 Replies)
Discussion started by: ooilinlove
5 Replies

6. Shell Programming and Scripting

How to remove all words starting from a matching word in a line

Hi Guys, I have a file like this: wwwe 1 ioie ewew yyy uuu 88 erehrlk 4 ihoiwhe lkjhassad lkhsad yyy mmm 45 jhash lhasdhs lkhsdkjsn ouiyrshroi oihoihswodnw oiyhewe yyy ggg 77 I want to remove everything after "yyy" and including "yyy" from each line in the file. So I want:... (2 Replies)
Discussion started by: npatwardhan
2 Replies

7. Shell Programming and Scripting

Remove particular word from file

Hi All, If my file is: Wed Sep 9 22:45:14 EDT 2009 sftp> sftp> sftp> sftp> sftp> sftp> sftp> sftp> sftp> sftp> sftp> sftp> sftp> This is log file generated from transfer... sftp> sftp> sftp> sftp> Files placed properly.... sftp> sftp> sftp> How can I remove "sftp>" word from this... (4 Replies)
Discussion started by: darshakraut
4 Replies

8. UNIX for Dummies Questions & Answers

Command to remove First and Last line from a File

I have a file from which the Header and the Trailer lines need to be removed. They are confirmed to be the first and the last lines in the file. I have tried a few commands, but not successful yet. It needs to be implemented urgently, hence any help is greatly appreciated. Raghu ----------... (1 Reply)
Discussion started by: ragz_82
1 Replies

9. UNIX for Dummies Questions & Answers

how to move word by word on command line

Hey All, On commad promt of a shell.. How can we move our cursor word by word. Like Ctrl+A takes to the starting of the command... Any shortcut like that..? Thanks pbsrinivas (1 Reply)
Discussion started by: pbsrinivas
1 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question