Add text in somewhere middle of line using information from the same line in file.
1
0
Entering edit mode
6.2 years ago
bk11 ★ 3.0k

I will appreciate if any one can help in this.

I have a file with lines like:

bigWigToBedGraph  http://epigenomesportal.ca/tracks/Blueprint/hg19/25378.Blueprint.ERS377505.H3K9me3.signal.bigWig -chrom=chr11 -start=118754475 -end=118766980

bigWigToBedGraph  http://epigenomesportal.ca/tracks/Blueprint/hg19/25384.Blueprint.ERS377537.H3K9me3.signal.bigWig -chrom=chr11 -start=118754475 -end=118766980

bigWigToBedGraph  http://epigenomesportal.ca/tracks/Blueprint/hg19/25391.Blueprint.ERS422247.H3K9me3.signal.bigWig -chrom=chr11 -start=118754475 -end=118766980

I want out put like this:

bigWigToBedGraph  http://epigenomesportal.ca/tracks/Blueprint/hg19/25378.Blueprint.ERS377505.H3K9me3.signal.bigWig 25378.Blueprint.ERS377505.H3K9me3.signal.bedGraph -chrom=chr11 -start=118754475 -end=118766980

bigWigToBedGraph  http://epigenomesportal.ca/tracks/Blueprint/hg19/25384.Blueprint.ERS377537.H3K9me3.signal.bigWig 25384.Blueprint.ERS377537.H3K9me3.signal.bedGraph -chrom=chr11 -start=118754475 -end=118766980

bigWigToBedGraph  http://epigenomesportal.ca/tracks/Blueprint/hg19/25391.Blueprint.ERS422247.H3K9me3.signal.bigWig 25391.Blueprint.ERS422247.H3K9me3.signal.bedGraph -chrom=chr11 -start=118754475 -end=118766980
sed awk unix • 1.8k views
ADD COMMENT
2
Entering edit mode

We appreciate the example output file, but it was difficult to see exactly what had changed. If you could be more explicit in future, that would be helpful.

This also seems a little like an XY problem. You may want to elaborate on the final goal, and we might be able to provide an even better solution.

ADD REPLY
1
Entering edit mode

What have you tried?

ADD REPLY
0
Entering edit mode

I have no idea how to do it.

ADD REPLY
2
Entering edit mode

You have tagged awk and sed, so you already know some tools which can approach this. You'll need to look into their usage and probably google a bit. If you master these tools you'll enormously increase your command line productivity. We appreciate if you show some effort. We are volunteers who gladly put you back on track, but we don't really like to solve issues without you trying first.

ADD REPLY
3
Entering edit mode
6.2 years ago
Joe 21k

Against my better judgement, as you should really try to learn these things, here's a pure bash way:

while IFS=' ' read -r -a array ; do
 newstring=$([[ ${array[1]} =~ ^.*/(.*)\. ]] && echo "${BASH_REMATCH[1]}")
 echo "${array[@]:0:2} ${newstring}.bedGraph ${array[@]:3:${#array[@]}}"
done < $1

Put it in a script file and then do:

bash script.sh inputfile.txt > outputfile.txt
ADD COMMENT
1
Entering edit mode

Slick! Thank you! It worked!

ADD REPLY
1
Entering edit mode

Why not a simple

sed -r 's|http://epigenomesportal.ca/tracks/Blueprint/hg19/([^ ]+).bigWig|http://epigenomesportal.ca/tracks/Blueprint/hg19/\1.bigWig \1.bedGraph|' input.txt
ADD REPLY
1
Entering edit mode

'Coz I like making life hard for myself, and pushing bash to unreasonable levels :P

ADD REPLY
0
Entering edit mode

Edit: updated with some improved bash magic (largely for my own amusement).

ADD REPLY

Login before adding your answer.

Traffic: 2109 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6