Extracting IP's from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting IP's from a file
# 1  
Old 08-18-2006
Extracting IP's from a file

I've been trying to work out a way to extract IP's from a file. The IP's are contained withing brackets like this:
[xxx.xxx.xxx.xx]
is there a way to extract just the IP's or at least everything from the starting to ending bracket with grep or something similar?

Thanks in advance.
# 2  
Old 08-18-2006
cat filename | cut -f2 -d [ | cut -f1 -d ] > new_filename
# 3  
Old 08-18-2006
Quote:
Originally Posted by DukeNuke2
cat filename | cut -f2 -d [ | cut -f1 -d ] > new_filename
is 'cat' really necessary?
# 4  
Old 08-18-2006
Quote:
Originally Posted by eth0
I've been trying to work out a way to extract IP's from a file. The IP's are contained withing brackets like this:
[xxx.xxx.xxx.xx]
is there a way to extract just the IP's or at least everything from the starting to ending bracket with grep or something similar?

Thanks in advance.
post a sample file and indentify what parts of it you want to extract.
# 5  
Old 08-18-2006
cut -f2 -d [ filename | cut -f1 -d ] > new_filename

this works also... and without the "cat"
# 6  
Old 08-18-2006
That won't work if the ip addresses are interspersed everywhere throughout the file, i.e. if the file looks like this:

Code:
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]

This (clunky) bit should work:
Code:
words=$(<file.txt)
for word in $words; do
  echo $word | grep "\[" | awk '{gsub("\[|\]","",$0); print}'
done

Code:
999.999.999.991
999.999.999.992
999.999.999.993
999.999.999.994
999.999.999.995
999.999.999.996
999.999.999.997

# 7  
Old 08-18-2006
Still clunky but faster and using shell built-ins and no additional processes
Code:
for word in $(<file.txt)
do
    [[ $word != \[* ]] && continue
    word=${word#\[}
    print ${word%\]}
done

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