Finding the line after (if it exists)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding the line after (if it exists)
# 1  
Old 12-13-2009
Finding the line after (if it exists)

Hi there, i have a text file like this


Code:
[node1]
          category=production
          memory=32gb
          CPU=2.6ghz
[node2]
          category=uat
          memory=16gb
          CPU=2.6ghz
[node3]
          memory=32gb
          CPU=2.6ghz
[node4]
          memory=8gb
          CPU=2.8ghz

Is there away I can do the equivelent of say

a) grep for one of the nodes
b) check the line directly after to see if it there is a "category" line
c) if so then set category to whatever it is set to in the line
d) if not then set it to "none"

I tried using awk but got myself in a real pickle and ended up going round in circles

could anyone give me some guidance on what i would use to do this ?

Cheers
# 2  
Old 12-13-2009
I did it this way:

Code:
awk '
  C && !($0 ~ /category=/) { print "          category=none" }
  1
  /^\[/ { C = 1; next }
  { C = 0 }
' input_file

[node1]
          category=production
          memory=32gb
          CPU=2.6ghz
[node2]
          category=uat
          memory=16gb
          CPU=2.6ghz
[node3]
          category="none"
          memory=32gb
          CPU=2.6ghz
[node4]
          category="none"
          memory=8gb
          CPU=2.8ghz

But it took me about 20 minutes to get a tilde on my Swiss Mac keyboard - probably the worst keyboard in the World - so I got cheesed off and couldn't be bothered with it any more!
# 3  
Old 12-13-2009
ha, thanks Scott, Im not sure i understand the syntax, but ill spend some time working it out ... thank you very much indeed
# 4  
Old 12-13-2009
Are you trying to add a line "cateory=none" to the file if it is not set? Or are you trying to set a variable called category to "none" or the category from the file if set? From your post it looks like you want to retrieve a specific node's category setting

---------- Post updated at 01:56 PM ---------- Previous update was at 01:53 PM ----------

If the later try this script, call it what you like and pass it a node number as an arg: -

Code:
category=$(nawk -F= -v pat="node"$1 '
  ( $0 ~ pat ){
    getline
    if( index( $1, "category") )
      print $2
    else
      print "none"
  }
' infile )
 
echo $category

# 5  
Old 12-13-2009
Guess rethink asked for is more simple. Just want to add none after category=, if no set after it.

Code:
$ cat urfile
[node1]
          category=production
          memory=32gb
          CPU=2.6ghz
[node2]
          category=
          memory=16gb
          CPU=2.6ghz
[node3]
          memory=32gb
          CPU=2.6ghz
[node4]
          memory=8gb
          CPU=2.8ghz

$ sed 's/category=$/&none/' urfile
[node1]
          category=production
          memory=32gb
          CPU=2.6ghz
[node2]
          category=none
          memory=16gb
          CPU=2.6ghz
[node3]
          memory=32gb
          CPU=2.6ghz
[node4]
          memory=8gb
          CPU=2.8ghz

# 6  
Old 12-14-2009
Try this:

Code:
sed -n '
'/node/' !p
'/node/' { N; s/memory.*/category=none\n\t&/g;p;} ' file

Output:

Code:
[node1]
          category=production
          memory=32gb
          CPU=2.6ghz
[node2]
          category=uat
          memory=16gb
          CPU=2.6ghz
[node3]
          category=none
          memory=32gb
          CPU=2.6ghz
[node4]
          category=none
          memory=8gb
          CPU=2.8ghz

# 7  
Old 12-14-2009
Steadyonabix, that is exactly what I was looking for and its also highly readable which is great.. I should have made it clearer that I needed to populate a variables as opposed to adding category= lines into the file ... thank you all for your guidance,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed/grep: check if line exists, if not add line?

Hello, I'm trying to figure out how to speed up the following as I want to use multiple commands to search thousands of files. is there a way to speed things up? Example I want to search a bunch of files for a specific line, if this line already exists do nothing, if it doesn't exist add it... (4 Replies)
Discussion started by: f77hack
4 Replies

2. Shell Programming and Scripting

Remove only specific char on every line when exists

Hi I need to remove "|" char when it's the last char of the line. Input file: generoso|desprendido|altruista| abnegar|ceder|sacrificar| abocetado-da|esbozado| apuntado|insinuado|incompleto abocetar|esbozar|bosquejar| diseņar|delinear ------------------------ output need --- ... (11 Replies)
Discussion started by: lookoo
11 Replies

3. Shell Programming and Scripting

Help with finding last line of file: if statement depending on that line.

Good morning, My first time actually posting in this forum, though I have used this forum to help with numerous projects. I am trying to figure out why my if statement does not work. I have a file where a line is inputted every 15 seconds. I want this if statement to check what the last line... (3 Replies)
Discussion started by: Shanrunt
3 Replies

4. Shell Programming and Scripting

Finding whether a value exists or not in a file

I am running a query against a database and spooling it to a file. I need to check to see if a certain value is in a specific field. I can use awk to check the specific field. However, part of the value changes. Part that does not change: MYPROD Right now I have OPS$MYPROD But... (4 Replies)
Discussion started by: guessingo
4 Replies

5. Shell Programming and Scripting

finding the line number of a particular line in a file

Hi Frnds, I need to find the line number of a particular line in a file and store that line number to a variable. if a file named myfile contains following look at the sun look at the moon look at the star look at the ocean i need to get the line number of the line 'look at the... (3 Replies)
Discussion started by: mvignesh
3 Replies

6. UNIX for Dummies Questions & Answers

Need help finding a file where a pattern exists and the file has a timestamp

So, I know how to do some of this stuff on an individual level, but I'm drawing a blank as to how to put it all together. I have a pattern that I'm looking for in a log file. The log file I know came in yesterday, so I want to limit the search to that day's listing of files. How would I do... (5 Replies)
Discussion started by: kontrol
5 Replies

7. Shell Programming and Scripting

How To comment a line where a word exists

Hi All Can u help me.. My problem is comment (#) a line where a word exists in that line sample: cat /tmp/file.txt monitor 192.168.1.11 Copying files in current directory 1 monitor 192.168.1.1 Copying files in current directory 2 monitor 192.168.1.12 Copying files in current... (2 Replies)
Discussion started by: darren_j
2 Replies

8. Shell Programming and Scripting

print line if 2nd field exists in text

2 files, first one has 3 fields seperated by ||| and 2nd one is plain text. I want to copy the lines from the first file if the 2nd field is present anywhere in the text file. This is what I've tried, but I'm new to awk and shell scripting in general so it's kinda broken. #!/bin/awk -f BEGIN... (15 Replies)
Discussion started by: FrancoisCN
15 Replies

9. Shell Programming and Scripting

how to get line number of different words if exists in same line

I have some txt files. I have to create another text file which contains the portion starting from the format "Date Sex Address" to the end of the file. While using grep -n on Date it also gives me the previous line containg Date. and also Date may be DATE in some files. My file is like this... (10 Replies)
Discussion started by: Amiya Rath
10 Replies

10. UNIX for Dummies Questions & Answers

Need to serach if a new line character exists on the last line in a file

I have a file in which I need to search if a new line character exists on the last line in the file. Please let me know how can I achieve it using Unix commands? (10 Replies)
Discussion started by: sunilbm78
10 Replies
Login or Register to Ask a Question