Extracting text from within a section of text using AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting text from within a section of text using AWK
# 1  
Old 10-25-2012
Extracting text from within a section of text using AWK

I have a command which returns the below output. How can I write a script to extract mainhost and secondhost from this output and put it into an array? I may sometimes have more hosts like thirdhost. I am redirecting this output to a variable. So I guess there should be a awk or sed command to extract it. But I am not able to figure it out.I am using the below command, which prints the secondhost.How can I change it to print mainhost as well.
Code:
echo $var | awk -F\" '{print $(NF-1)}'


Code:
{
    "outcome" => "success",
    "result" => [
        "mainhost",
        "secondhost"
    ]
}

# 2  
Old 10-25-2012
Code:
$ awk -F\" '/"result"/ { getline; print $2; getline; print $2 }' file1
mainhost
secondhost

A variant of your original:
Code:
echo $var | awk -F\" '{print $(NF-1), $(NF-3)}'

This User Gave Thanks to Scott For This Post:
# 3  
Old 10-25-2012
Thanks Scott for your quick reply. It works when I have mainhost and secondhost. Sometimes I will have more than two hosts like below. Its random and depends on the environment I am running. So how can I make it work whether it has 2 hosts or more.

Code:
{
    "outcome" => "success",
    "result" => [
        "mainhost",
        "secondhost"
        "thirdhost"
    ]
}

# 4  
Old 10-25-2012
Code:
$ cat myScript
awk -F\" '/"result"/ { P=1; next }
  /]/ { P=0 }
  P { X=(X?X " " $2:$2) }
  END { print X }
' file1

$ cat file1
{
    "outcome" => "success",
    "result" => [
        "mainhost",
        "secondhost"
        "thirdhost"
    ]
}

$ ./myScript
mainhost secondhost thirdhost

This User Gave Thanks to Scott For This Post:
# 5  
Old 10-25-2012
Thanks Scott! It works perfectly.
# 6  
Old 10-25-2012
You're welcome!

If you're looking for your thread, I've renamed it to "Extracting text from within a section of text using AWK". "Scripting help" isn't very meaningful Smilie
# 7  
Old 10-25-2012
also, if any other formats of "results" in file1 like:
Code:
{
    "outcome" => "success",
    "result" => [ "mainhost", "secondhost", "thirdhost", "fourthost", "manyhosts" ]
}

try:
Code:
hosts=($(sed -n '/"result".*\[/,/\]/p' file1 | tr -d '\n' | sed 's/.*\[ *//;s/ *\].*//; s/ *, */ /g;'))
echo ${hosts[@]}
"mainhost" "secondhost" "thirdhost" "fourthost" "manyhosts"

This User Gave Thanks to rdrtx1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: passing shell variables through and extracting text

Hello, new to the forums and to awk. Glad to be here. :o I want to pass two shell (#!/bin/sh) variables through to awk and use them. They will determine where to start and stop text extraction. The code with the variables hard-coded in awk works fine; the same code, but with the shell... (7 Replies)
Discussion started by: bedtime
7 Replies

2. Shell Programming and Scripting

awk to skip lines find text and add text based on number

I am trying to use awk skip each line with a ## or # and check each line after for STB= and if that value in greater than or = to 0.8, then at the end of line the text "STRAND BIAS" is written in else "GOOD". So in the file of 4 entries attached. awk tried: awk NR > "##"' "#" -F"STB="... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Grep text and see all section

Hello I am looking for a way to look in files and to grep text and see the all section of the text ! Sharon123 deed 10000 class 360 ! sharon456 2000 deed ! Sharon789 live 3000 ! To grep "deed "an see the all section (5 Replies)
Discussion started by: sharong
5 Replies

4. UNIX for Dummies Questions & Answers

Extracting 22-character strings from text using sed/awk?

Here is my task, I feel sure this can be accomplished with see/awk but can't seem to figure out how. I have large flat file from which I need to extract every case of a pairing of characters (GG) in this case PLUS the previous 20 characters. The output should be a list (which I plan to make... (17 Replies)
Discussion started by: Twinklefingers
17 Replies

5. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

6. Shell Programming and Scripting

Extracting text between two strings

Hi, I've looked at a few existing posts on this, but they don't seem to work for my inputs. I have a text file where I want to extract all the text between two strings, every time that occurs. Eg my input file is Anna said that she would fetch the bucket. Anna and Ben moved the bucket.... (9 Replies)
Discussion started by: JamesForeman
9 Replies

7. UNIX for Dummies Questions & Answers

how can i extract text section via grep

hi, i have a very long text file. i need to extract with grep command a certain part. for example text file include 1ooo rows: 1.... 2... 3... . . . 1000 i want to view with grep only rows 50-100. any ideas will be appreciated thanks... (8 Replies)
Discussion started by: meny
8 Replies

8. Shell Programming and Scripting

Extracting Text

Hi, I have a file like that No seq Text 269091 123443124 how are slkslks, serial no is 042890000 and pincode is 090001223433454 269091 123443124 how are slkslks, Sr/no is 0428901 and 14 digits are is ... (3 Replies)
Discussion started by: krabu
3 Replies

9. UNIX for Dummies Questions & Answers

extracting text and reusing the text to rename file

Hi, I have some ps files where I want to ectract/copy a certain number from and use that number to rename the ps file. eg: 'file.ps' contains following text: 14 (09 01 932688 0)t the text can be variable, the only fixed element is the '14 ('. The problem is that the fixed element can appear... (7 Replies)
Discussion started by: JohnDS
7 Replies

10. Shell Programming and Scripting

Sorting rules on a text section

Hi all My text file looks like this: start doc ... (certain number of records) REC3|Emma|info| REC3|Lukas|info| REC3|Arthur|info| ... (certain number of records) end doc start doc ... (certain number of records)... (4 Replies)
Discussion started by: Indalecio
4 Replies
Login or Register to Ask a Question