LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-08-2011, 02:57 PM   #16
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,029

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845

Code:
grep -v ^$ lethalfang.txt
# this essentially removes any line that begins with nothing.

edit: oops i read it wrong... i'll comeback with another try.

this works but the previous suggestions are probably better:
Code:
echo 1 > lethalfang.left
grep -n ^$ lethalfang.txt | cut -d : -f 1 | sed -n 1~2p > lethalfang.rite
grep -n ^$ lethalfang.txt | cut -d : -f 1 | sed -n 2~2p >> lethalfang.left
wc -l lethalfang.txt | cut -d " " -f 1 >> lethalfang.rite
for range in `paste -d , lethalfang.left lethalfang.rite`
do
 sed -n "$range"p lethalfang.txt
done
rm lethalfang.left lethalfang.rite
nope: it fails on your post #5... still fine tuning.


for something like this you are probably better off doing something custom in c.

Last edited by schneidz; 07-08-2011 at 03:45 PM.
 
Old 07-09-2011, 12:48 AM   #17
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
May I ask if there is an input scenario that my awk did not work with?
 
Old 07-09-2011, 08:38 PM   #18
lethalfang
LQ Newbie
 
Registered: Jun 2011
Location: San Francisco, CA
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Well its not pretty and can probably be condensed, but this seems to work:
Code:
awk 'x && NF{ y=1 }y{ print x }{if(NF)x = $0;else{ if(y)print; x = y = 0}}END{if(y)print x}' file
Yep, this one works very well. Thanks!

My own code kinda works, but inserted way too many empty lines in the output file (although I can tolerate that issue)... not to mention it took 1000X the time.
This code is perfect.

Last edited by lethalfang; 07-09-2011 at 08:40 PM.
 
Old 07-09-2011, 08:42 PM   #19
lethalfang
LQ Newbie
 
Registered: Jun 2011
Location: San Francisco, CA
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
Getting close to the edge of what you can reasonably call a "one-liner", but yes:
Code:
awk '{l3=l2;l2=l1;l1=$0}NR>=2&&!(l3==""&&l2!=""&&l1==""){print l2}END{print}' $file >> Duplicate_$file
This code works really well, too.
Not as great as the one below , but a lot better than the one I wrote myself.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove lines in text file that contain two '@' symbols xsyntax Linux - Newbie 5 12-07-2009 06:58 PM
Remove lines in a text file based on another text file asiandude Programming 10 01-29-2009 11:59 AM
Adding lines of text to beginning of a text file BillKat Programming 2 01-19-2009 11:40 AM
Grab text lines in text file LULUSNATCH Programming 1 12-02-2005 11:55 AM
Remove odd lines from a text file Mr. Gone Programming 2 09-19-2005 12:16 PM


All times are GMT -5. The time now is 05:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration