last day (14 days later) » 

1:06 AM
1
A: Getting extra line when output to file?

SandyElmsTo delete empty last line you can use sed, it will delete it only if it's blank: sed '${/^\s*$/d;}' file

 
It doesn't work. What am I doing wrong?
 
it depends on how you using it and if your last line really blank
 
I just tried echoing a random string into the file and it still adds an extra line. echo asdf > test.txt. By extra line I mean it's just blank, as if you've hit enter (or return) and just stopped. Just a new line.
 
use sed = file . it will show number for each line. I have file with 10 lines, last line is empty, i run sed i provided and see 9 lines.
 
It says 605 but when I open it in Sublime Text it tells me there are 606 lines.
 
1:06 AM
try this: sed '${/^\s*$/d;}' file > newfile and then check new file.
 
Alright, thanks by the way.
 
one more thing i wanted to know
 
So I tried out the last solution you suggested. Same thing.
 
is these files created in windows?
 
Ask away.
Nope.
I'm using Mac OSX 10.10
 
1:07 AM
got it
could you show me the final script you have?
 
I simply tried echo abc > test.txt
To make things simpler. I get the same result.
 
to help you i need to see the whole solution you have, probably you have an error in code
 
I go into terminal and type in echo abc > test.txt
The whole solution is a bit too complicated.
Alright, so, I use this.
diff -b <(grep -B 2 -A 1 $answer /Applications/MAMP/conf/apache/httpd.conf) /Applications/MAMP/conf/apache/httpd.conf | grep -e '^> ' | sed -e 's/^> //' | sed '${/^\s*$/d;}' > test.txt
To print the final solution.
Oh, and answer is bedrock.local or newserver
$answer
 
can you tell why do you use this sed -e 's/^> //' ?
 
To remove the ">" that would appear at the beginning of every line as a result of the grep -e '^>'
 
1:14 AM
you don't really remove it
you substitute it
 
Oh, yeah.
Though the ">" doesn't appear at the new line.
Let me upload the file to show you what I mean.
 
ok
and you don't need -e to use s with sed
 
let me check
 
Alright, removed the -e with sed
 
1:21 AM
you don't have to use grep -e '^> ' , sed will find and replace these >
the file you showed, you receive it after diff right?
 
Yup.
Just the diff without the grep or the sed after.
There's also an odd number at the beginning of the file. Does that do anything?
 
not really, it shows how many lines are different
first file, second file
 
Ah, the number at the top only disappears if grep and sed are used subsequently.
 
you know what
try to diff first
 
What?
 
1:27 AM
and then save to file
 
Okay.
 
after that grep what you need and sed
 
I tried to diff alone.
That's the file I just showed you "test.txt"
 
aha
 
Yup.
 
1:28 AM
but it doesn't have blank line at the bottom
or you got it after grep?
 
Of course there is.
At the very bottom there's an extra line.
That's blank.
Just a space.
Not a space, but just a new line.
 
one moment
 
Alright.
 
ok i saved it
it hase 657 lines
after i do sed on it and save it to new file rightaway i have 656 lines
 
Hmmm...
 
1:32 AM
like this sed '${/^\s*$/d;}' test.txt > new.txt
 
Yeah, but I think it recreates the line for me.
Let me check one more time just to be safe.
Yup.
Same result.
 
sed itself ony change stdout, it doesn't change the file
only*
 
Ah.
It's quite odd.
Alright, um...
 
you need to add -i if you want sed to write changes to file
if you don't redirect stdout to a file
 
Do you know how I would go about appending (>>) to a file while ignoring the last line? (As in overwrite the last line if it's blank)
Hm, let me check that one.
 
1:37 AM
i can check your problem at home once again
i have mac with the same version of OS at home
 
Oh, alright.
I get this error: sed: 1: "test.txt": undefined label 'est.txt'
When I use -i
 
sed -i 's///' test.txt
that's how it should be done
and your pattern inside s
 
Ah, with the Mac I had to specify a backup file name after -i
 
just do it like -i.bak
 
Yeah, it worked, but the line is still there.
 
1:44 AM
i'll check at home with this blank stuff
 
Anyways, I feel like this is too much of a silly problem to worry about it anymore. I'll mark your answer as the correct one, because it actually is. I think it's something silly I'm overlooking.
 
probably it's mac
 
Let me know if you're still intrigued and want to update me about it. I'll look for another workaround.
I'm guessing so.
 
if i find the root , i'll update the answer
 
Thank you. I really appreciate it.
 
1:45 AM
we just learn everyday)
 
But of course.
I'm off to bed now. It's 5AM here... </3 Goodnight!
 

last day (14 days later) »