Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I'm trying to write a script, and the first part requires one line of a text document to be changed. The script asks: "ID?" and the user types in an ID number, then (hopefully) the script awks it over into the right line of the text file. I originally thought it would be easy to say "Insert $ID into line 14 of file1" but it seems not to be.
A proper example would be really nice. I'm not even sure if I should be using awk.
Thanks
-viceroy
To get the user id, I would use: read -p
To get rid of extra parts of that, I would use: cut
To insert into a particular line of file, I would use: sed -i
I'm trying to write a script, and the first part requires one line of a text document to be changed. The script asks: "ID?" and the user types in an ID number, then (hopefully) the script awks it over into the right line of the text file. I originally thought it would be easy to say "Insert $ID into line 14 of file1" but it seems not to be.
A proper example would be really nice. I'm not even sure if I should be using awk.
Thanks
-viceroy
Actually this worked in my test, but the actual script didn't like it. I'm really inexperienced, and this script is mainly a learning experience. Right now I have:
echo "Enter ID group, separating each integer with a space.
(EX: 8 5 0 4 3 2 2 1)"
read ID1 ID2 ID3 ID4 ID5 ID6 ID7 ID8 ID9
awk -v ID1=$ID1 'NR==39{ print ID1} {print}' "LOG"
awk -v ID2=$ID2 'NR==40{ print ID2} {print}' "LOG"
awk -v ID2=$ID3 'NR==41{ print ID3} {print}' "LOG"
awk -v ID2=$ID4 'NR==42{ print ID4} {print}' "LOG"
awk -v ID2=$ID5 'NR==43{ print ID5} {print}' "LOG"
awk -v ID2=$ID6 'NR==44{ print ID6} {print}' "LOG"
awk -v ID2=$ID7 'NR==45{ print ID7} {print}' "LOG"
awk -v ID2=$ID8 'NR==46{ print ID8} {print}' "LOG"
awk -v ID2=$ID9 'NR==47{ print ID9} {print}' "LOG"
echo "Hopefully $ID1 $ID2 $ID3 $ID4 $ID5 $ID6 $ID7 $ID8 $ID9 was inserted."
This is kind of ridiculous. I ask for spaces because I don't know how to cut up a string of integers. The biggest problem, though, is that awk isn't inserting the text.
Thanks.
Actually this worked in my test, but the actual script didn't like it. I'm really inexperienced, and this script is mainly a learning experience. Right now I have:
echo "Enter ID group, separating each integer with a space.
(EX: 8 5 0 4 3 2 2 1)"
read ID1 ID2 ID3 ID4 ID5 ID6 ID7 ID8 ID9
awk -v ID1=$ID1 'NR==39{ print ID1} {print}' "LOG"
awk -v ID2=$ID2 'NR==40{ print ID2} {print}' "LOG"
awk -v ID2=$ID3 'NR==41{ print ID3} {print}' "LOG"
awk -v ID2=$ID4 'NR==42{ print ID4} {print}' "LOG"
awk -v ID2=$ID5 'NR==43{ print ID5} {print}' "LOG"
awk -v ID2=$ID6 'NR==44{ print ID6} {print}' "LOG"
awk -v ID2=$ID7 'NR==45{ print ID7} {print}' "LOG"
awk -v ID2=$ID8 'NR==46{ print ID8} {print}' "LOG"
awk -v ID2=$ID9 'NR==47{ print ID9} {print}' "LOG"
echo "Hopefully $ID1 $ID2 $ID3 $ID4 $ID5 $ID6 $ID7 $ID8 $ID9 was inserted."
This is kind of ridiculous. I ask for spaces because I don't know how to cut up a string of integers. The biggest problem, though, is that awk isn't inserting the text.
Thanks.
Won't work from line 41 onwards because you assign to ID2
but are trying to insert ID3-9 instead of ID2.
Won't work from line 41 onwards because you assign to ID2
but are trying to insert ID3-9 instead of ID2.
What result *do* you get?
Um...wow. Whoops.
The results were not very interesting. Nonexistent, actually.
After fixing that, now the last integer gets inserted. But 39 - 46 are still AWOL. I must admit this is an improvement, but I don't know what I'm missing.
Actually this worked in my test, but the actual script didn't like it. I'm really inexperienced, and this script is mainly a learning experience. Right now I have:
echo "Enter ID group, separating each integer with a space.
(EX: 8 5 0 4 3 2 2 1)"
read ID1 ID2 ID3 ID4 ID5 ID6 ID7 ID8 ID9
awk -v ID1=$ID1 'NR==39{ print ID1} {print}' "LOG"
awk -v ID2=$ID2 'NR==40{ print ID2} {print}' "LOG"
awk -v ID2=$ID3 'NR==41{ print ID3} {print}' "LOG"
awk -v ID2=$ID4 'NR==42{ print ID4} {print}' "LOG"
awk -v ID2=$ID5 'NR==43{ print ID5} {print}' "LOG"
awk -v ID2=$ID6 'NR==44{ print ID6} {print}' "LOG"
awk -v ID2=$ID7 'NR==45{ print ID7} {print}' "LOG"
awk -v ID2=$ID8 'NR==46{ print ID8} {print}' "LOG"
awk -v ID2=$ID9 'NR==47{ print ID9} {print}' "LOG"
echo "Hopefully $ID1 $ID2 $ID3 $ID4 $ID5 $ID6 $ID7 $ID8 $ID9 was inserted."
This is kind of ridiculous. I ask for spaces because I don't know how to cut up a string of integers. The biggest problem, though, is that awk isn't inserting the text.
Thanks.
you don't have to call so many awk commands. not tested yet
Code:
echo "enter id : a b c "
read -a array
arr=${array[@]}
awk -v arr=$arr '{
n=split($0,arr)
if (NR==39) { print arr[1] }
else if ( NR==40 ) { print arr[2] }
else if ( NR==41 ) { print arr[3] }
else if ( NR==42 ) { print arr[4] }
.... #and so on
{ print }
}' "file"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.