Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

learn grep,need help

Original Message
Name: iancaem02
Date: April 17, 2008 at 02:58:53 Pacific
Subject: learn grep,need help
OS: linux
CPU/Ram: 256
Model/Manufacturer: dell
Comment:
I am a newbee in shell scripts and I have the need to write a loop but I can't figure it out.
I have file trial.sql that contain several lines :
a111
a112
a113
....
axxx

I have a second file trial.sh , i want to grep those strings from the first file and execute it one by one (add strings to those strings)..
after execute :
a111@abc.net
a112@abc.net
a113@abc.net
............
axxx@abc.net
what should i wrote on trial.sh

any direction would be greatly appreciated.

BEST REGARDS
ADRIAN


Report Offensive Message For Removal


Response Number 1
Name: ernie
Date: April 17, 2008 at 15:17:51 Pacific
Subject: learn grep,need help
Reply: (edit)
If trial.sql is a straight text file containing the lines as you report above, the cat command will be a better tool to use than grep because grep is intended to search files for specific strings while cat will print the contents of a file to standard output (the terminal screen). In a terminal window, run man grep and man cat to get more information on these commands.

I suggest you use a for loop to process each line. The format of a for loop is:

for variable-name in list
do commands
done

or

for variable-name in list;do commands; done (all on a single line)

The for loop puts one item from list in variable-name with each iteration, so if the list you provide is the output of the cat command when run on ~/trial.sql, you can append @abc.net with the echo command then redirect the output to a temporary file, rename (or delete) the original file, then rename the temporary file as the original file like this:

--------------
#!/bin/bash

#trial.sh

# Use cat to output the contents of trial.sql
# to stdout.
# Run a for loop to append abc.net to each
# line of output from cat and save the result
# in a temporary file.

for a in $(cat ~/trial.sql)
do echo $a@abc.net >> ~/trial.temp
done

# Rename trial.sql as trial.sql.old.

mv ~/trial.sql ~/trial.sql.old

# Finally, rename trial.temp as trial.sql

mv ~/trial.temp ~/trial.sql

# end
end-------------

This is a quick and dirty script, and I am sure there are more elegant ways to get the job done (I tried to make as much as clear as I could), but I wanted to make this as educational as possible.

The tilde (~) in the path names (~/trial.sql) is shorthand for /home/$USER (the current user's home directory).

The for loop takes the output of $(cat ~/trial.sql) and puts one line at a time in the variable a ($a) then runs the command echo $a@abc.net >> ~/trial.temp which does the following:

outputs the contents of the variable ($a) and the text @abc.net (no spaces) to standard output (the terminal screen).

redirects (>>) the output of the echo command and appends it to the end of the file ~/trial.temp

This process is repeated for each iteration (line) of the for loop.

mv ~/trial.sql ~/trial.sql.old renames the original file ~/trial.sql as ~/trial.sql.old so the original information is preserved.

If you do not want to preserve the original information, you can replace this line with:

rm -f ~/trial.sql

The -f option to the rm command forces deletion of the target with no user interaction.

mv ~/trial.temp ~/trial.sql renames the temporary file as the original file (~/trial.sql).

If this is not what you wanted, or if the contents of the trial.sql file is not exactly as you posted, this may not work as expected.

As a test, open a terminal window, and run the command cat ~/trial.sql (or use the full path to the file if it is not in your user's home directory) to make sure the output is as described in your original post.

HTH,

Ernie Registered Linux User 247790
ICQ 41060744


Report Offensive Follow Up For Removal

Response Number 2
Name: iancaem02
Date: April 18, 2008 at 03:38:25 Pacific
Subject: learn grep,need help
Reply: (edit)
ernie thanx so lot,it works..

okay, lets study!?!

BEST REGARDS
ADRIAN


Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: learn grep,need help

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




WMP11 and mpg ?

BIOS for processors

Install XP

Distorted sound with visualizations

strange noise frm hdd external


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC