LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-25-2013, 02:16 AM   #1
ChocaMocha
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Rep: Reputation: Disabled
Shell script to remove selected line number from text file.


deleted.

Last edited by ChocaMocha; 04-28-2013 at 11:28 PM.
 
Old 04-25-2013, 04:20 AM   #2
ChocaMocha
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
also deleted.

Last edited by ChocaMocha; 04-28-2013 at 11:28 PM.
 
Old 04-25-2013, 02:11 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Good for you.

But sed should work as well, if you have the syntax right.

Code:
line=5
sed -i "$line d" infile
sed -i "${line}d" infile
All you have to do is make sure the variable is in double-quotes, so that it will expand, and that there's a syntactical separation between it and the one-letter command following it. This can be a space, or you can use the full form of the variable.

The nice thing about sed (the gnu version at least), is that you can use the -i option to edit the original file in place.

BTW, even your awk command can be made cleaner:
Code:
awk -v "line=$rec" 'NR != line' data.txt > temp.txt
mv temp.txt data txt
Shell variables should generally be copied into awk variables via the -v option.


A few other suggestions too.

1) Instead of a while loop, try using a select loop. It's designed especially for doing this kind of thing.

2) You have multiple read commands all looking at the same input. That's not very safe. I suggest configuring the individual actions as functions and calling those instead. You may have to supply separate file descriptors for the input too (pointing to /dev/tty).

3) Instead of using printf, use read's "-p" option to supply a prompt.

4) Speaking of which, the proper syntax for printf has a format string as the first argument and the data to be printed as the following ones. While it will certainly work the way you did it, as long as you're printing a single string only, it's not really correct. Code and data should be kept separate whenever possible.

Code:
printf '%s: ' 'Price'                # or use echo -n
printf '%s\n' 'Edit sales entries'   # or just use echo
5) Consider using nl or "cat -n" to list the file with prefixed line numbers.

Last edited by David the H.; 04-25-2013 at 02:21 PM. Reason: minor fixes + addition
 
Old 04-25-2013, 02:17 PM   #4
ChocaMocha
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
not deleted. lol jk its deleted.

Last edited by ChocaMocha; 04-28-2013 at 11:28 PM.
 
Old 04-25-2013, 03:11 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
We all start out there, so no worries. Just keep plugging away and you'll get it in time.

If you haven't seen the BashGuide yet, I highly recommend taking some time off to read it thoroughly. It, and the rest of the site, really helped me clean up my act. Be sure to read through the pitfalls and faq too, to learn about the common errors to avoid.

http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashPitfalls
http://mywiki.wooledge.org/BashFAQ
 
  


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
Editing And Deleting A Line From A Text File Using Shell Script shiftshifter Linux - Newbie 6 07-13-2012 01:18 PM
need to write a shell script for appending text into each line of a file nandakv4 Programming 9 08-15-2009 03:32 AM
shell script loop though text file:-1 line at a time. knockout_artist Linux - Newbie 2 05-04-2008 06:58 PM
Shell script - how to show a specific line of a text file davi_cabral Linux - Software 3 09-28-2004 01:39 PM
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 11:18 AM.

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