Cut ad delete


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut ad delete
# 1  
Old 08-31-2009
Cut ad delete

Hi i need to cut some rows matching an ip, delete it and redirect all on an other file. I did this:
Code:
cat all_devices.log | awk '/192.168.0.254/' > routerA.log
sed -i '/192.168.0.254/d' all_devices.log

Now i want to rewrite all in a single sed command. But i can't find the best way. Any suggestion?

Thanks
# 2  
Old 08-31-2009
Please post sample data and required output.
# 3  
Old 08-31-2009
Quote:
Originally Posted by danmero
Please post sample data and required output.
Ok!
I have this log:
Aug 31 08:54:23 Parallels[92]: Loading Virtual Ethernet module...
Aug 31 08:54:32 Parallels[104]: Initialization complete.
Aug 31 08:54:32 192.168.0.254 yyyyyyyyyy xxxxxx
Aug 31 08:54:52 192.168.0.254 : yyyyyyyyyyyy zzzzzzzzzz
Aug 31 08:54:31 Parallels[97]: Staring DHCP/NAT daemon...

And i want to extract the 192.168.0.254 rows and copy that in an other logfile.
The "clean" log should be like this:
Aug 31 08:54:23 Parallels[92]: Loading Virtual Ethernet module...
Aug 31 08:54:32 Parallels[104]: Initialization complete.
Aug 31 08:54:31 Parallels[97]: Staring DHCP/NAT daemon...

and this is the new log:
Aug 31 08:54:32 192.168.0.254 yyyyyyyyyy xxxxxx
Aug 31 08:54:52 192.168.0.254 : yyyyyyyyyyyy zzzzzzzzzz

But i'm trying to do this in ony one command row.
# 4  
Old 08-31-2009
Hi.

Code:
awk '/192.168.0.254/ {print > "file1"; next} { print > "file2"}' infile

# 5  
Old 08-31-2009
Sed -i (in-place) can lead to unexpected results. I'll suggest to use a temporary file.
Code:
awk '{split($4,d,".");file=(d[4]?$4:"new")".log";print >> file;close(file)}' old.log && mv new.log old.log

This is just a basic example Smilie
# 6  
Old 09-02-2009
This will work for any ip

Code:
awk ' ($4 ~ /[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/ ) {print > "file1" ;next } {print > "file2"}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using :<<cut / cut to comment out block of bash script

I am using : << cut / cut to comment out block of code. Works fine on few lines of script, then it gives me this cryptic error when I try to comment out about 80 lines. The "warning " is at last line of script. done < results 169 echo "END read all positioning parameters" 170... (8 Replies)
Discussion started by: annacreek
8 Replies

2. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

3. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies

4. Shell Programming and Scripting

delete to end of formatted warnings using sed or cut...

hi, i've searched the forums' entries and have tried some of the examples -- to no avial -- this is my first post -- thanks in advance for your help... As part of an installation program -- i'm receiving two(2) extraneous "libcxb WARNING!" statements -- i want to use sed to eliminate the... (12 Replies)
Discussion started by: rickkar
12 Replies
Login or Register to Ask a Question