mscice
1
hello i need some help here are the contents of my file.
test.txt
this is filename 1.mp3 http://www.url.com/filenamehashed
filename 2.mp3 http://www.url.com/fileamehashed
something_else.zip http://www.url.com/filenamehashed
so this file has 100 of these lines
filename url
I would like to alter this file so the output is this:
filename
url
empty space
filename
url
emptyspace
filename
url
emptyspace
etc etc
how would i go about doing this? one liners would be perfect.
thank you for your help
you can use awk
awk '{printf "%s\n%s\n\n",$1,$2}' inputfile
the file names contains spaces as shown in his sample, therefore using $1 and $2 might not be appropriate.
@OP, if you have Python, an alternative solution
#!/usr/bin/env python
for line in open("file"):
line=line.strip().split()
filename = ' '.join(line[:-1])
url=line[-1]
print filename
print url
print
output
# ./test.py
this is filename 1.mp3
http://www.url.com/filenamehashed
filename 2.mp3
http://www.url.com/fileamehashed
something_else.zip
http://www.url.com/filenamehashed
With sed:
cat infile
this is filename 1.mp3 http://www.url.com/filenamehashed
filename 2.mp3 http://www.url.com/fileamehashed
something_else.zip http://www.url.com/filenamehashed
% sed 's|\(.*\)\(http://.*\)|\1\
\2\
|' infile
this is filename 1.mp3
http://www.url.com/filenamehashed
filename 2.mp3
http://www.url.com/fileamehashed
something_else.zip
http://www.url.com/filenamehashed
thank you all for your ideas and replies..
i tried the sed one
sed 's|\(.*\)\(http://.*\)|\1\\2\|' test.txt
sed: -e expression #1, char 29: unterminated `s' command
is my syntax wrong?
you did a typo error look closely what radoulove written
full code is not written in single line
perl:
while(<DATA>){
my @tmp=split(/ (?=http.*)/,$_);
print $tmp[0],"\n";
print $tmp[1];
print "\n";
}
__DATA__
this is filename 1.mp3 http://www.url.com/filenamehashed
filename 2.mp3 http://www.url.com/fileamehashed
something_else.zip http://www.url.com/filenamehashed
Or:
print join "\n", m|(.*)(http://.*)|, "\n"
while <DATA>;
__DATA__
this is filename 1.mp3 http://www.url.com/filenamehashed
filename 2.mp3 http://www.url.com/fileamehashed
something_else.zip http://www.url.com/filenamehashed
sed 's|\(.*\)\(http://.*\)|\1\\2\|' infile
Can you please explain this command.
Thanks in advance
panyam
10
Go through the below link. Which is having very useful information about sed.
www.grymoire.com/Unix/Sed.html