LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-16-2010, 01:37 AM   #1
Abid Malik
LQ Newbie
 
Registered: Sep 2010
Posts: 25

Rep: Reputation: -1
Question delete a line from file using shell script


Hello Everyone!

i have a file containing contents as

1 2 3
2 2 3
11 2 3

now i want to delete the line which first column content is 1. so, if i pass the parametere 1 to function delete_row. is should remove the first row as

2 2 3
11 2 3

and then write the contents in file.
 
Old 10-16-2010, 01:53 AM   #2
Kenny_Strawn
Senior Member
 
Registered: Feb 2010
Location: /usa/ca/orange_county/lake_forest
Distribution: ArchBang, Google Android 2.1 + Motoblur (on Motortola Flipside), Google Chrome OS (on Cr-48)
Posts: 1,791
Blog Entries: 62

Rep: Reputation: 56
If you have a patch file for the script that has a remove operator you could apply the patch to the file to delete the first line.
 
1 members found this post helpful.
Old 10-16-2010, 02:01 AM   #3
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
If you want to delete the line from the file without copying it, then perhaps sed can help you:

sed -i '/^1 / d' file

seems to work on your test input. The space between the 1 and the / is crucial. Obviously, you'd need to pass a variable from your shell script in place of the 1 if that could change. Oh and note that the i option is for in place change - i.e., it will change the file it is operating on.

Please test things first! I'm not responsible for any data loss!

Last edited by Nylex; 10-16-2010 at 02:04 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script to Delete line if pattern exists topcat Programming 22 08-23-2011 04:58 AM
Shell Script to Delete part text of a line if pattern matches harsha1980 Programming 36 04-16-2010 03:36 AM
unix shell script:How to delete the first line in a file?? rche3252 Programming 6 03-03-2010 07:32 AM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 08:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration