LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-07-2017, 11:18 PM   #1
kbashyal
LQ Newbie
 
Registered: Oct 2017
Posts: 2
Blog Entries: 2

Rep: Reputation: Disabled
replacing nth line of a file with a new line containing different sets of values


The 32nd line of my file is "25 25 25 1 1 1".
I want to create 7 similar files but by replacing this 32nd line with " $i $i $i 1 1 1".
Where $i = 17,19,21,23,25,27,29

can anyone please suggest anything??
 
Old 10-07-2017, 11:22 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,767

Rep: Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571Reputation: 3571
sed
 
Old 10-07-2017, 11:48 PM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,655
Blog Entries: 3

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
I second the recommendation to look at sed for this. It will do what you describe. You might consider the -r option to make things easier to read.

If you are new to sed, then try here:

http://www.grymoire.com/Unix/Sed.html
https://www.gnu.org/software/sed/manual/sed.html
 
1 members found this post helpful.
Old 10-08-2017, 12:16 AM   #4
!!!
Member
 
Registered: Jan 2017
Location: Fremont, CA, USA
Distribution: Trying any&ALL on old/minimal
Posts: 759

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Welcome to LQ. You'll notice a "Rule" is: post what you have tried.
I add: web-research (&post your keywords); gets lots of info instantly=faster

One very valuable technique that's often overlooked is: break it into simple pieces!!!
Here, start with i=17 then a single simple sed 32 print ...
to get the sed part working. Separately, write a bash for loop, that just echo each $i
Then, put them together. Use CODE tags to post what you try, and results.

Best wishes!!! Let us know... (interview? homework? What class?)

More: here's my web-search keywords (1st result might be sed solution!!!)
How to replace the n-th line in a file with a new line in Unix?

&1more: I had glitch on passing $i in single-quotes, and d then i on last line, so:
Code:
i=17;echo old | sed -e '1a'{$i}'' -e 1d   #hints: "32a$i $i 1" #n=32; ... ${n}d 
i=17;n=2;seq 3| awk "NR==$n {print $i,$i,1}; NR!=$n"  #beats sed imho

Last edited by !!!; 10-08-2017 at 03:28 AM.
 
1 members found this post helpful.
Old 10-08-2017, 01:38 AM   #5
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 945

Rep: Reputation: 216Reputation: 216Reputation: 216
If the files are small and the assignment is one-time, then just open it in your favourite text editor, make the changes and <save as ..> 7 times

OK
 
Old 10-08-2017, 06:30 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,765

Rep: Reputation: 796Reputation: 796Reputation: 796Reputation: 796Reputation: 796Reputation: 796Reputation: 796
An attempt with sed
Code:
#!/bin/sh
file=inputfile
for i in 17 19 21 23 25 27 29
do
  sed '
    32 {
      s/[^ ]\{1,\}/'$i'/1
      s/[^ ]\{1,\}/'$i'/2
      s/[^ ]\{1,\}/'$i'/3
    }
  ' $file > $file.$i
done
 
1 members found this post helpful.
Old 10-08-2017, 11:20 AM   #7
kbashyal
LQ Newbie
 
Registered: Oct 2017
Posts: 2

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
Thank you it worked.
#!/bin/sh
file=inputfile
for i in 17 19 21 23 25 27 29
do
sed '
32 {
s/[^ ]\{1,\}/'$i'/1
s/[^ ]\{1,\}/'$i'/2
s/[^ ]\{1,\}/'$i'/3
}
' $file > $file.$i
done

This one exactly worked for my problem.
 
Old 10-08-2017, 11:24 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,655
Blog Entries: 3

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Interesting. I would have done it with the i command:

Code:
sed -e "32d; 33i$i $i $i 1 1 1"
I guess the code golf can now commence.
 
Old 10-08-2017, 01:24 PM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,765

Rep: Reputation: 796Reputation: 796Reputation: 796Reputation: 796Reputation: 796Reputation: 796Reputation: 796
I found it more challenging to retain the other fields.
(A classroom instructor will certainly smell a rat.)

Tc and others replace the whole line.
Can be done better with a s(substitute) command
Code:
sed "32 s/.*/$i $i $i 1 1 1/"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
AWK/BASH: get nth line from a file by getline feed to actions in a same awk line cristalp Programming 3 11-23-2011 11:38 AM
print lines form nth line to mth line which fulfill specific condition cristalp Programming 4 11-07-2011 07:39 AM
print nth line after the line which matches the string cristalp Programming 7 10-27-2011 01:53 PM
[SOLVED] Copy and replacing specific line from file1 to file2 line by line vjramana Programming 10 03-28-2011 07:49 AM
nth line of a file in perl kadhan Programming 1 02-20-2008 11:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:18 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