Add text in somewhere middle of line using information from the same line in file.
1
0
Entering edit mode
3.9 years ago
bk11 ▴ 60

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 • 960 views
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.

1
Entering edit mode

What have you tried?

0
Entering edit mode

I have no idea how to do it.

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.

3
Entering edit mode
3.9 years ago
Joe 20k

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

1
Entering edit mode

Slick! Thank you! It worked!

0
Entering edit mode

Good good, I forgot to add that your input files will need to not have any blank lines (unlike your examples above).

0
Entering edit mode

How about for this type of lines:

http://ftp.ebi.ac.uk/pub/databases/blueprint/data/homo_sapiens/GRCh38/bone_marrow/BM220513/segmented_neutrophil_of_bone_marrow/ChIP-Seq/NCMLS/S00G3YH1.ERX651365.Input.bwa.GRCh38.20150528.bw
http://ftp.ebi.ac.uk/pub/databases/blueprint/data/homo_sapiens/GRCh38/cord_blood/C00184/mature_neutrophil/ChIP-Seq/NCMLS/C00184H2.ERX197163.Input.bwa.GRCh38.20150528.bw


To get:

http://ftp.ebi.ac.uk/pub/databases/blueprint/data/homo_sapiens/GRCh38/bone_marrow/BM220513/segmented_neutrophil_of_bone_marrow/ChIP-Seq/NCMLS/S00G3YH1.ERX651365.Input.bwa.GRCh38.20150528.bw segmented_neutrophil_of_bone_marrow.bedGraph
http://ftp.ebi.ac.uk/pub/databases/blueprint/data/homo_sapiens/GRCh38/cord_blood/C00184/mature_neutrophil/ChIP-Seq/NCMLS/C00184H2.ERX197163.Input.bwa.GRCh38.20150528.bw mature_neutrophil.bedGraph

1
Entering edit mode

Please invest some time and learn awk - it'll be really useful.

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

1
Entering edit mode

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

0
Entering edit mode

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