Finding a character in first line of a record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding a character in first line of a record
# 1  
Old 07-17-2007
Finding a character in first line of a record

HI,

I am pretty new to Unix scripting. I will need help in Finding a character in first line of a file or a set of files.

The scenario is as follows:

Lets consider a set of files which is having a character "ID"(without quotes) in the first line of each file.I need to find this character in the first line of each file and if the result of this is positive then i have to say echo"ID found in file" otherwise echo "ID not found in the file"

Can anyone help me with this. I will be very GratefullSmilie

Thanks,
Sandy
# 2  
Old 07-17-2007
is it homework? if so, have a look at the rules!
# 3  
Old 07-18-2007
NO!!!! this is not a home work.......
# 4  
Old 07-18-2007
I have tried searching this through google from past three days.....but i am not able to find any answers on this.......can anyone help me out ...

Thanks,
Sandy
# 5  
Old 07-18-2007
Perhaps this.

Code:
while read filename
do
  line=$(head -1 $filename)
  if [[ $line = *ID* ]] ; then
    echo "Found ID in $filename"
  else
    echo "No ID in $filename"
  fi;
done < list_of_filenames.txt

# 6  
Old 07-18-2007
Code:
awk 'NR==1{ if ( $0 ~ /ID/) print }' "file"

# 7  
Old 07-18-2007
Thanks so much.....u guys are awesome.......i hope that i am also as good as u guys and help someone in this forum in the comming months.

Thanks and regards,
Sandy
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding the Latest record

Dear All, I have getting data as follows, the second field signifies table name and last one is time stamp. I have return always latest record based on time stamp. Could you please help me ? I/P ==== ... (1 Reply)
Discussion started by: srikanth38
1 Replies

2. Shell Programming and Scripting

Finding a certain character in a filename and count the characters up to the certain character

Hello, I do have folders containing having funny strings in their names and one space. First, I do remove the funny strings and replace the space by an underscore. find . -name '* *' | while read file; do target=`echo "$file" | sed 's/... (2 Replies)
Discussion started by: tempestas
2 Replies

3. Shell Programming and Scripting

Adding end of line character in the last record

Need to add end of line character to last record in a fixed width file. When i take the record length of each line, for all the records it gives example like 200 except the last line as 199. Due to this my other script fails. Please help me on this. (4 Replies)
Discussion started by: Amrutha24
4 Replies

4. Shell Programming and Scripting

How to Remove the new line character inbetween a record

I have a file, in which a single record spans across multiple lines, File 1 ==== 14|\n leave request \n accepted|Yes| 15|\n leave request not \n acccepted|No| I wanted to remove the '\n charecters. I used the below code (foudn somewhere in this forum) perl -e 'while (<>) { if... (1 Reply)
Discussion started by: machomaddy
1 Replies

5. Shell Programming and Scripting

finding the incorrect record

Hi , I have a scenario where i have to find the incorrect records in the file. In our comma delimited file , we have the following to be taken care : 1) if there is new line character then we have to capture the current line and the next line as error record Ex : In a comma... (8 Replies)
Discussion started by: ashwin3086
8 Replies

6. Shell Programming and Scripting

Reject the record if the record in the next line does not begin with 2.

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten 2eleven 2twelve 1thirteen 2fourteen The output should be: (5 Replies)
Discussion started by: supchand
5 Replies

7. Shell Programming and Scripting

Reject the record if the record in the next line does not satisfy the pattern

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten The output should be 1one 2two 3three 1four 2five 3six (2 Replies)
Discussion started by: supchand
2 Replies

8. Shell Programming and Scripting

Finding longest line in a Record

Good Morning/Afternoon All, I am using the nawk utility in korn shell to find the longest field and display that result. My Data is as follows: The cat ran The elephant ran Milly ran too We all ran I have tried nawk '{ if (length($1) > len) len=length($1); print $1}' filename The... (5 Replies)
Discussion started by: SEinT
5 Replies

9. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

10. Shell Programming and Scripting

Character count per record

I have a flat file. How can i retrive the character count per record in whole file. Can anybody assist me on this Cheers (9 Replies)
Discussion started by: subrat
9 Replies
Login or Register to Ask a Question