Visit Our UNIX and Linux User Community


Error while appending records to a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error while appending records to a file
# 1  
Old 07-24-2009
Error while appending records to a file

Hi,
I have a sample file which contains records.
Input File :
1 user1 username1\password@database-name
2 user2 username2\password@database-name
3 user3 username1\password@database-name
I should search for a 'username1\' in those records. If 'username1\' is found in those records, that record should be appended to a new file.
output file :
1 user1 username1\password@database-name
3 user3 username1\password@database-name
For the above requirement I have written a script as below.
#!/bin/ksh
set -x
cd directory
for file in `cat filename`
do
if [ -f *username1\* ]
then
$file >> outputfile
else
continue
fi
done
set +x
But when I execute the script it is not appeneding the records to output file. Please help me regarding this.
Thanks in advance.
# 2  
Old 07-24-2009
Code:
 
grep username1 inputfile >>outputfile

Incase if you want to do the search with backslash.

Code:
grep username1\\\\ inputfile >>outputfile


Last edited by lathavim; 07-24-2009 at 07:56 AM..
# 3  
Old 07-24-2009
Thanks

Thanks for the answer.... it worked. But if we want to search for more than one user how we can proceed ?

grep username1 | username4 filename > outputfile

It is throwing an error that ksh:username4 not found. The situation is we dont know who ever will logn and what records I can get in that file. Please tell me how to find use the same command for different users ?
# 4  
Old 07-24-2009
Code:
 grep -E 'username1|username4'  filename

# 5  
Old 07-24-2009
Thanks

thanks for ur answer.But when i typed the command:

grep -E 'username1|username4' inputfile>>outputfile

it gave an error that: illegal option -- E
Usage: grep -hblcnsviw pattern file . . .


Thanks in advance,
# 6  
Old 07-24-2009
Hi, This should work for ksh.

Code:
 
#!/bin/ksh
grep -E 'username1|username4'  filename >output

What shell are you using?

Last edited by lathavim; 07-24-2009 at 08:35 AM..
# 7  
Old 07-24-2009
Quote:
Originally Posted by lathavim
Hi, This should work for ksh.

Code:
 
#!/bin/ksh
grep -E 'username1|username4'  filename >output

What shell are you using?
try this--
Code:
grep -e 'username1' -e 'username4'  filename >output


Previous Thread | Next Thread
Test Your Knowledge in Computers #546
Difficulty: Medium
In computer science, an operator takes the values of its operands and combines them in some way to produce a new value.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Separate records of a file on 2 types of records

Hi I am new to shell programming in unix Please if I can provide help. I have a file structure of a header record and "N" detail records. The header record will be the total number of detail records I need to split the file in 2: One for the header Another for all detail records Could... (1 Reply)
Discussion started by: jamcogar
1 Replies

2. Shell Programming and Scripting

Extract error records based on specific criteria from Unix file

Hi, I look for a awk one liner for below issue. input file ABC 1234 abc 12345 ABC 4567 678 XYZ xyz ght 678 ABC 787 yyuu ABC 789 7890 777 zxr hyip hyu mno uii 678 776 ABC ty7 888 All lines should be started with ABC as first field. If a record has another value for 1st... (7 Replies)
Discussion started by: ratheesh2011
7 Replies

3. Shell Programming and Scripting

Ascii Mode appending extra records to csv file

I am relatively new to this forum and Unix scripting. ksh script: part 1 :will call a PL\SQL program will create 3 CSV file at the unix directory. part 2 : will sftp the files to the EFT server. Once the EFT server receives these file , it will transfer them to a shared windows folders. ... (3 Replies)
Discussion started by: developerpa
3 Replies

4. Shell Programming and Scripting

Deleting duplicate records from file 1 if records from file 2 match

I have 2 files "File 1" is delimited by ";" and "File 2" is delimited by "|". File 1 below (3 record shown): Doc1;03/01/2012;New York;6 Main Street;Mr. Smith 1;Mr. Jones Doc2;03/01/2012;Syracuse;876 Broadway;John Davis;Barbara Lull Doc3;03/01/2012;Buffalo;779 Old Windy Road;Charles... (2 Replies)
Discussion started by: vestport
2 Replies

5. Shell Programming and Scripting

Splitting record into multiple records by appending values from an input field (AWK)

Hello, For the input file, I am trying to split those records which have multiple values seperated by '|' in the last input field, into multiple records and each record corresponds to the common input fields + one of the value from the last field. I was trying with an example on this forum... (4 Replies)
Discussion started by: imtiaz99
4 Replies

6. Shell Programming and Scripting

Capturing the invalid records to error file

HI, I have a source file which has the below data. Tableid,table.txt sourceid,1,2,3,4,5,6 targetid,1,2,3,4,5,6 Tableid,table sourceid,1,2,3,4,5,6 targetid,1,2,3,4,5,6 Tableid,table.txt sourceid,1,2,3,4,5,6 targetid,1,2,3,4,5,6 Tableid,table sourceid,1,2,3,4,5,6 targetid,1,2,3,4,5,6... (6 Replies)
Discussion started by: shruthidwh
6 Replies

7. UNIX for Dummies Questions & Answers

Grep specific records from a file of records that are separated by an empty line

Hi everyone. I am a newbie to Linux stuff. I have this kind of problem which couldn't solve alone. I have a text file with records separated by empty lines like this: ID: 20 Name: X Age: 19 ID: 21 Name: Z ID: 22 Email: xxx@yahoo.com Name: Y Age: 19 I want to grep records that... (4 Replies)
Discussion started by: Atrisa
4 Replies

8. Shell Programming and Scripting

Appending error messages from log file next to the corresponding error record

Hi Everyone, I have an issue and trying to get a solution but was not succesful yet. Any help is greatly appreciated. I am using ksh to inoke sql loader to load data from txt file into two oracle tables based on the condition written in the control file. If an error occurs while loading into... (8 Replies)
Discussion started by: vpv0002
8 Replies

9. UNIX for Dummies Questions & Answers

Appending error

Hi All, I just want to append the value in variable at the end of the file. var=1234 sed -e "$a $var" file1 > file 2. But I get this error sed: -e expression #1, char 4: unknown command: `1' Kindly let m know how can I do that... (5 Replies)
Discussion started by: waqar1
5 Replies

10. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies

Featured Tech Videos