LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 07-20-2007, 06:55 PM   #1
viceroy
LQ Newbie
 
Registered: Jul 2007
Posts: 5

Rep: Reputation: 0
Awk- search & replace text


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
 
Old 07-20-2007, 08:59 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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
 
Old 07-20-2007, 09:19 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by viceroy
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
here's how you can do it in awk:
Code:
echo "Enter ID: "
read ID
awk -v ID=$ID 'NR==14{ print ID}{print}' "file"
 
Old 07-20-2007, 11:16 PM   #4
viceroy
LQ Newbie
 
Registered: Jul 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74
here's how you can do it in awk:
Code:
echo "Enter ID: "
read ID
awk -v ID=$ID 'NR==14{ print ID}{print}' "file"
Thanks, that worked!
 
Old 07-21-2007, 05:02 PM   #5
viceroy
LQ Newbie
 
Registered: Jul 2007
Posts: 5

Original Poster
Rep: Reputation: 0
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.
 
Old 07-21-2007, 11:39 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by 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.
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?


Cheers,
Tink
 
Old 07-22-2007, 12:33 AM   #7
viceroy
LQ Newbie
 
Registered: Jul 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Tinkster
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.

Thanks for the help.
 
Old 07-22-2007, 10:18 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by 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.
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"
 
  


Reply

Tags
homework



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
Need command to search and replace text in file acascianelli AIX 12 04-11-2007 08:16 PM
another question about search and replace text graziano1968 Linux - General 3 08-02-2006 09:35 AM
How to search and replace a text using grep DediPlace Linux - General 2 05-29-2005 06:47 PM
search for specific text in fields using awk Helene Programming 2 04-23-2004 12:13 AM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 01:56 PM

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

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