Extracting IP's from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting IP's from a file
# 8  
Old 08-18-2006
Wierd. I get no results when I use your code:
Code:
for word in $(<ip.txt)
do
    if [[ $word = \[* ]]; then
      word=${word#\[}
      print ${word%\]}
    fi
done

But it works if I do
Code:
for word in $(<ip.txt)
do
    if [[ $word = \[[0-9]* ]]; then
      word=${word#\[}
      print ${word%\]}
    fi
done

# 9  
Old 08-18-2006
Quote:
Originally Posted by Glenn Arndt
Wierd. I get no results when I use your code:
Curious, what shell are you using?
# 10  
Old 08-18-2006
ksh88. I can tell that (for whatever reason) it's not matching $word to \[* but it does match it to \[[0-9]*. The xtrace is showing
Code:
[3]+ [[ foo = [* ]]              
[3]+ [[ [999.999.999.991] = [* ]]
[3]+ [[ [999.999.999.992] = [* ]]
[3]+ [[ bar = [* ]]

...which looks like it should match. But it doesn't.
# 11  
Old 08-18-2006
Now I see it. My example was testing for inequality yours is test for equality.
# 12  
Old 08-18-2006
Well, yes. I actually misrepresented your code, which really was:
Code:
for word in $(<file.txt)
do
    [[ $word != \[* ]] && continue
    word=${word#\[}
    print ${word%\]}
done

It doesn't return anything for me. If however, I only add the [0-9], as in:
Code:
for word in $(<file.txt)
do
    [[ $word != \[[0-9]* ]] && continue
    word=${word#\[}
    print ${word%\]}
done

It works. Why is that?
# 13  
Old 08-18-2006
Quote:
Originally Posted by Glenn Arndt
Well, yes. I actually misrepresented your code, which really was:
Code:
for word in $(<file.txt)
do
    [[ $word != \[* ]] && continue
    word=${word#\[}
    print ${word%\]}
done

It doesn't return anything for me.
I don't know why unless our input files are different.
Code:
 $ cat file.txt 
foo [999.999.999.991] [999.999.999.992] bar
[999.999.999.993]
foo
[999.999.999.994] foo
foo bar
foo [999.999.999.995]
[999.999.999.996] foo bar [999.999.999.997]

The proof:
Code:
{
set -x
for word in $(<file.txt)
do
    [[ $word != \[* ]] && continue
    word=${word#\[}
    print ${word%\]}
done
set +x
}
+ 0< file.txt
+ [[ foo != [* ]]
+ continue
+ [[ [999.999.999.991] != [* ]]
+ word=999.999.999.991]
+ print 999.999.999.991
999.999.999.991
+ [[ [999.999.999.992] != [* ]]
+ word=999.999.999.992]
+ print 999.999.999.992
999.999.999.992
+ [[ bar != [* ]]
+ continue
+ [[ [999.999.999.993] != [* ]]
+ word=999.999.999.993]

Quote:
Originally Posted by Glenn Arndt
If however, I only add the [0-9], as in:
Code:
for word in $(<file.txt)
do
    [[ $word != \[[0-9]* ]] && continue
    word=${word#\[}
    print ${word%\]}
done

It works. Why is that?
That is self evident in your example's case. It should work but I can't answer why mine won't work for you. I too am using KSH88.
# 14  
Old 08-18-2006
That's bizarre. Our input files are identical. Your code gives me:
Code:
[1]+ [[ foo != [* ]]              
[3]+ continue                     
[3]+ [[ [999.999.999.991] != [* ]]
[3]+ continue                     
[3]+ [[ [999.999.999.992] != [* ]]
[3]+ continue                     
[3]+ [[ bar != [* ]]              
[3]+ continue                     
[3]+ [[ [999.999.999.993] != [* ]]
[3]+ continue                     
[3]+ [[ foo != [* ]]              
[3]+ continue                     
[3]+ [[ [999.999.999.994] != [* ]]
[3]+ continue                     
[3]+ [[ foo != [* ]]              
[3]+ continue                     
[3]+ [[ foo != [* ]]              
[3]+ continue                     
[3]+ [[ bar != [* ]]              
[3]+ continue                     
[3]+ [[ foo != [* ]]              
[3]+ continue                     
[3]+ [[ [999.999.999.995] != [* ]]
[3]+ continue                     
[3]+ [[ [999.999.999.996] != [* ]]
[3]+ continue                     
[3]+ [[ foo != [* ]]              
[3]+ continue                     
[3]+ [[ bar != [* ]]              
[3]+ continue                     
[3]+ [[ [999.999.999.997] != [* ]]
[3]+ continue

Maybe I have something set differently?

Update:
It works if I quote the \[:
Code:
for word in $(<file.txt)
do
    [[ $word != "\["* ]] && continue
    word=${word#\[}
    print ${word%\]}
done


Last edited by Glenn Arndt; 08-18-2006 at 03:07 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Extracting data from one file, based on another file (splitting)

Dear All, I have two files but want to extract data from one based on another... can you please help me file 1 David Tom Ellen and file 2 David|0010|testnamez|resultsz David|0004|testnamex|resultsx Tom|0010|testnamez|resultsz Tom|0004|testnamex|resultsx Ellen|0010|testnamez|resultsz... (12 Replies)
Discussion started by: A-V
12 Replies

2. UNIX for Dummies Questions & Answers

Extracting line1 from a file with certain file pattern in line 7

Hello there, I am new to unix and would like to do the following, hoping someone would give some guide, thanks in advance. Lets say i have a file like this: A w x y w x 0.1 B w x y w x 0.3 C w x y w x 0.7 D w x y w x 0.9 E w x y w x 0.2 So i would like to extract line 1 data where line... (2 Replies)
Discussion started by: seiksoon
2 Replies

3. Shell Programming and Scripting

reading a file extracting information writing to a file

Hi I am trying to extract information out of a file but keep getting grep cant open errors the code is below: #bash #extract orders with blank address details # # obtain the current date # set today to the current date ccyymmdd format today=`date +%c%m%d | cut -c24-31` echo... (8 Replies)
Discussion started by: Bruble
8 Replies

4. Shell Programming and Scripting

extracting line numbers from a file and redirecting them to another file

i have applied the following command on a file named unix.txt that contains the string "linux from the text" grep -n -i -w "linux from the text" unix.txt and the result is 5:Today's Linux from the text systems are split into various branches, developed over time by AT&T as well as various... (5 Replies)
Discussion started by: arindamlive
5 Replies

5. Shell Programming and Scripting

Extracting a column from a file and merging with other file using awk

Hi All: I have following files: File 1: <header> text... text .. text .. text .. <\header> x y z ... File 2: <header> text... text .. text .. (4 Replies)
Discussion started by: mrn006
4 Replies

6. Shell Programming and Scripting

extracting columns using pattern file from source file

Hi experts,Please help me for the below requirement. i have a source file.(lets say contains 50 columns). I am extarcting five columns from the source file by using pattern file. for example input file:--------a,b,c,d,"a,g","v b",s,koutputfile=======a,"a,g","v b",s,kThanks in advance subhendu (2 Replies)
Discussion started by: subhendu81
2 Replies

7. Shell Programming and Scripting

Parsing file, yaml file? Extracting specific sections

Here is a data file, which I believe is in YAML. I am trying to retrieve just the 'addon_domains" section, which doesnt seem to be as easy as I had originally thought. Any help on this would be greatly appreciated!! I have been trying to do this in awk and mostly bash scripting instead of perl... (3 Replies)
Discussion started by: Rhije
3 Replies

8. Shell Programming and Scripting

Extracting data from text file based on configuration set in config file

Hi , a:) i have configuration file with pattren <Range start no>,<Range end no>,<type of records to be extracted from the data file>,<name of the file to store output> eg: myfile.confg 9899000000,9899999999,DATA,b.dat 9899000000,9899999999,SMS,a.dat b:) Stucture of my data file is... (3 Replies)
Discussion started by: suparnbector
3 Replies

9. Shell Programming and Scripting

Extracting from file

Hi I have the file in following format Begining of file --------------------------------------- my name some dfgfgfk jdksjdkls laladsl sdlsdls . . . kfdjkfdk some drt pro vhdl sdjls. ---------------------------------------------------------------- ddr.spw.df.df 0 0 0 0 0 ddr.ser.ddf.tp... (7 Replies)
Discussion started by: hack_tom
7 Replies

10. Shell Programming and Scripting

[Splitting file] Extracting group of segments from one file to others

Hi there, I need to split one huge file into separate files if the condition is fulfilled according to that the position between 97 and 98 matches with “IT” at the segment MAS. There is no delimiter file is fix-width with varous line length. Could you please help me how I do split the file... (1 Reply)
Discussion started by: ozgurgul
1 Replies
Login or Register to Ask a Question