Remove lone lines from a text file
Hey, anyone has ideas how to remove lone lines from a text file?
If I have a file that is like this: ----------------------------------- line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 line 10 ----------------------------------- What command(s) will remove the lone lines of this file, i.e., line 4 and line 7? Thanks in advance. |
Slightly modified from the sed FAQ here:
Code:
sed ': more;$!N;s/\n/&/2;t enough;$!b more;: enough;/^\n.*\n$/d;P;D' file Code:
sed ': more;$!N;s/\n/&/2;t enough;$!b more;: enough;s/^\n.*\n$/\n/;P;D' file Code:
sed ': more;$!N;s/\n/&/2;t enough;$!b more;: enough;s/^\n.*\n$//;P;D' file |
Try: $ sed -i.bk '/^$/ d' myfile
|
Quote:
|
Quote:
------ line 1 line 2 line 3 line 4 line 5 line 6 ------ The script gets rid of line 3, but not line 4. Is there any way to get rid of all lone lines at once? Thanks. |
Quote:
Try: Code:
# grep . myfile |
Quote:
I'm wondering if I can get rid of the lines that are empty above and below. |
Guess I'm confused on what you mean by "lone lines". I assumed you meant empty lines, but are you saying you want to remove specific lines? As in "remove arbitrary line X and Y" from a set of input?
|
Quote:
I actually wrote a tedious and rudimentary script to do that. It kinda works, but it's totally inefficient. I can write some rudimentary bash scripts, but I'm not all that good at it. Code:
#!/bin/bash "./identify_duplicates.sh: line 22: [: too many arguments" for this line of code "if [ $val_i = $empty_var -a $val_k = $empty_var ]" |
You can add
Code:
set -xv |
Quote:
The error messages went away when I changed Code:
if [ $val_i = $empty_var -a $val_k = $empty_var ] Code:
if [ "$val_i" = "$empty_var" -a "$val_k" = "$empty_var" ] Now does anyone have a more efficient one-liner for that stuff? :-) |
Quote:
Code:
[ -z "$val_i" -a -z "$val_k" ] && cat $file | awk 'NR=='$line_j'' >> Duplicate_$file |
Quote:
Code:
awk '{l3=l2;l2=l1;l1=$0}NR>=2&&!(l3==""&&l2!=""&&l1==""){print l2}END{print}' $file >> Duplicate_$file |
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 |
Re post #11 Double Brackets [[ ]] work better http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
|
All times are GMT -5. The time now is 06:47 AM. |