Question: Add text in somewhere middle of line using information from the same line in file.
0
gravatar for bk11
19 months ago by
bk1140
bk1140 wrote:

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
awk unix sed • 494 views
ADD COMMENTlink modified 19 months ago by Joe16k • written 19 months ago by bk1140
2

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 REPLYlink written 19 months ago by Joe16k
1

What have you tried?

ADD REPLYlink written 19 months ago by WouterDeCoster43k

I have no idea how to do it.

ADD REPLYlink written 19 months ago by bk1140
2

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 REPLYlink written 19 months ago by WouterDeCoster43k
3
gravatar for Joe
19 months ago by
Joe16k
United Kingdom
Joe16k wrote:

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 COMMENTlink modified 19 months ago • written 19 months ago by Joe16k
1

Slick! Thank you! It worked!

ADD REPLYlink written 19 months ago by bk1140
1

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 REPLYlink modified 19 months ago • written 19 months ago by RamRS27k
1

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

ADD REPLYlink written 19 months ago by Joe16k

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

ADD REPLYlink written 19 months ago by Joe16k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1666 users visited in the last hour