Check for lines in a file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check for lines in a file.
# 1  
Old 04-02-2008
Check for lines in a file.

I have been looking for a good method to do the following. I would like to check a file to verify two lines exist that start with yyy and verify other lines that start with yyy do not exist in file test1. If the other lines starting with yyy do exist I would like the script to echo “not remove” the lines that were found that should not exist. I would also like to verify and echo the lines starting with yyy that should exist. I want lines yyy111 and yyy222 and check for lines yyy*.

Example file contents:
aaa111
bbb111
ccc111
yyy111
yyy222
yyy333
yyy444
yyy555
yyy666

Thanks for any help.
Elijah
# 2  
Old 04-02-2008
Well, where are you stuck? What have you tried so far?
# 3  
Old 04-02-2008
From what I understood, you have a file containing lines like:
Code:
aaa111
bbb111
ccc111
yyy111
yyy222
yyy333
yyy444
yyy555
yyy666

and then you want to echo "not remove" in front of the lines with yyy111 and yyy222 and "whatever" (whatever might be empty-string!!) in front of the other "yyy" lines. If this is what you want to do, try this:
Code:
awk '{if ($0~/^yyy111/ || $0~/^yyy222/) print "not remove "$0; else if ($0~/^yyy/) print "whatever"$0; else print $0;}' file.txt

# 4  
Old 04-02-2008
Bug

Thanks unilover.
I was looking at using grep sending to a VAR and getting over complicated with it. I definitely need to learn to use the power of awk. This is what I needed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find all lines in file such that each word on that line appears in at least n lines of the file

I have a file where every line includes four expressions with a caret in the middle (plus some other "words" or fields, always separated by spaces). I would like to extract from this file, all those lines such that each of the four expressions containing a caret appears in at least four different... (9 Replies)
Discussion started by: uncleMonty
9 Replies

2. Shell Programming and Scripting

To check Blank Lines, Blank Records and Junk Characters in a File

Hi All Need Help I have a file with the below format (ABC.TXT) : ®¿¿ABCDHEJJSJJ|XCBJSKK01|M|7348974982790 HDFLJDKJSKJ|KJALKSD02|M|7378439274898 KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999 (cont......) I need to write a script where it will check for : blank lines (between rows,before... (6 Replies)
Discussion started by: chatwithsaurav
6 Replies

3. Shell Programming and Scripting

Check/Parse log file's lines using time difference/timestamp

I was looking at this script which outputs the two lines which differs less than one sec. #!/usr/bin/perl -w use strict; use warnings; use Time::Local; use constant SEC_MILIC => 1000; my $file='infile'; ## Open for reading argument file. open my $fh, "<", $file or die "Cannot... (1 Reply)
Discussion started by: cele_82
1 Replies

4. UNIX for Dummies Questions & Answers

to check the latest lines of log in vi editor itself..

Hi Folks, If I open the logs in vi editor but as the logs get update after few seconds and I want to see the latest lines then whats the command to see the latest lines in vi editor itself , lets say I have open a log named abc.log in vi..!! vi abc.log (2 Replies)
Discussion started by: SankalpS
2 Replies

5. Shell Programming and Scripting

Check last lines of different files

Hey, Each night, about 2000 network devices are being backupped. So in different folders, i've got each night 2000 new text files. And normally, in these files, on the last 5 lines, the word 'end' or 'return' should be found. If not, it means that the backup failed. Is there an easy way... (3 Replies)
Discussion started by: brononius
3 Replies

6. UNIX for Advanced & Expert Users

Check EOF char in Unix. OR To check file has been received completely from a remote system

Advance Thanks. (1) I would like to know any unix/Linux command to check EOF char in a file. (2) Or Any way I can check a file has been reached completely at machine B from machine A. Note that machine A ftp/scp the file to machine B at unknown time. (5 Replies)
Discussion started by: alexalex1
5 Replies

7. Shell Programming and Scripting

Script to Check & Edit Content of a file (Addition of comma in each lines of code)

Hi all, I need to write an automated bash shell script which performs such operations: 1. Grep the header of everyline with the initial of "T" in "FILE_A" 2. Perform a for loop, Count the numbers of comma in the line of code, if (no. of comma < 17) ADD the comma until 17; ... (2 Replies)
Discussion started by: big_nutz
2 Replies

8. Shell Programming and Scripting

Check numeric fields greater than zero, and delete lines if appropriate

This be the latest in my problems sorting through router logs... I'm half way there on a problem, but I've hit the limitation of my knowledge Got some router interface log files of type router01:GigabitEthernet9/24 is up, line protocol is up (connected) router01: 0 input errors, 0 CRC, 0... (7 Replies)
Discussion started by: Yorkie99
7 Replies

9. Shell Programming and Scripting

check position of end of line for some specific lines

-------------------------------------------------------------------------------- Have to check in a file that the lines starting with 620 and 705 are ending at same posiotin. 82012345 62023232323 70523949558 62023255454 9999 In the above lines, i have to check the lines starting... (1 Reply)
Discussion started by: senthil_is
1 Replies

10. UNIX for Dummies Questions & Answers

Script to check for a file, check for 2hrs. then quit

I wish to seach a Dir for a specific file, once the file is found i will perform additional logic. If the file is not found within two hours, i would like to exit. Logically, I'm looking for the best way to approach this Thanks for any assistance in advance. Note: I'm using a C shell and... (2 Replies)
Discussion started by: mmarsh
2 Replies
Login or Register to Ask a Question