Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Thanks. This kinda works, but when there are multiple lone lines, it seems to only delete one at a time. For example:
------
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.
I actually found a ridiculously easy way to do this a while back that made me say "duh" at the way I'd been doing it (complex sed commands and whatnot).
Try:
Code:
# grep . myfile
Then you can redirect that to a temp file and remove the old one if necessary ...
I actually found a ridiculously easy way to do this a while back that made me say "duh" at the way I'd been doing it (complex sed commands and whatnot).
Try:
Code:
# grep . myfile
Then you can redirect that to a temp file and remove the old one if necessary ...
This just gets rid of empty lines?
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?
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?
Yep. Basically, if a line has an empty line both above and beneath, I want that line removed.
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
file=$1
# Get the total number of lines in the file
num_lines=$(cat $file | wc -l)
line_j=1
empty_var=""
while [ $line_j -le $num_lines ]
do
# line_i is the line before line_j, and line_k is the line after line_j.
line_i=$(( $line_j - 1 ))
line_k=$(( $line_j + 1 ))
# see if those lines are empty
val_i=$(cat $file | awk 'NR=='$line_i'' | awk '{print $1}' )
val_k=$(cat $file | awk 'NR=='$line_k'' | awk '{print $1}' )
if [ $val_i = $empty_var -a $val_k = $empty_var ]
then true
else
cat $file | awk 'NR=='$line_j'' >> Duplicate_$file
fi
line_j=$(( $line_j + 1 ))
done
Besides, the terminal keep popping up
"./identify_duplicates.sh: line 22: [: too many arguments"
for this line of code
"if [ $val_i = $empty_var -a $val_k = $empty_var ]"
Last edited by lethalfang; 07-07-2011 at 04:00 PM.
as the 2nd line to see what the script is actually doing.
Ahh, that's a good tip.
The error messages went away when I changed
Code:
if [ $val_i = $empty_var -a $val_k = $empty_var ]
into
Code:
if [ "$val_i" = "$empty_var" -a "$val_k" = "$empty_var" ]
The issue seemed to be that, when $val_i has an non-empty value, say, "STUFF," the code was reading '[' STUFF = ']', i.e., something being compared to nothing. It's a messed up inequality, but the equality test failed anyway, so the previous code did its job.
Now does anyone have a more efficient one-liner for that stuff? :-)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.