bob123
September 13, 2017, 9:31am
1
im trying to get
awk to print the string between 3rd and 4th backslashs to end of line
test could be any word
this
http://example.com/test/ >
to this
http://example.com/test/ > test
also the other way round insert string at end of line between 3rd and 4th backslashs
thanks
RudiC
September 13, 2017, 9:51am
2
Any attempts / ideas / thoughts from your side?
bob123
September 13, 2017, 10:36am
3
hi
i know how to print string to end of line like this
awk '{print $0"append_to_end"}' file > file1
but the string between 3rd and 4th backslashs is ramdom
thats where im stuck
would like to also do it the other way round
appreciate your help thanks
Hello bob123,
Could you please try following and let me know if this helps you.
sed 's/\(.[^//]*\)\(\/\/\)\(.[^/]*\)\(\/\)\(.[^\/]*\)\(.*\)/\1\2\3\4\5\6 \5/' Input_file
Thanks,
R. Singh
1 Like
bob123
September 13, 2017, 10:59am
5
hi RavinderSingh13,
yeah that works
many thanks
i did close the space between 6 and 5 as i already had a space
sed 's/\(.[^//]*\)\(\/\/\)\(.[^/]*\)\(\/\)\(.[^\/]*\)\(.*\)/\1\2\3\4\5\6\5/' file
would you know how to do it the other way round
insert the string at the end between 3rd and 4th backslashs
Hello bob123,
Could you please try following and let me know if this helps you.
awk -F"/" '{print $0,$4}' Input_file
Thanks,
R. Singh
bob123
September 13, 2017, 11:50am
7
ravindersingh13:
Hello bob123,
Could you please try following and let me know if this helps you.
awk -F"/" '{print $0,$4}' Input_file
Thanks,
R. Singh
hi RavinderSingh13
yeah that works the same as the sed one you posted
thanks
any way to do it the other way round
Hello bob123,
Could you please try following approaches too and let me know if this helps you.
Solution 1st: Using awk
.
awk '{val=$0;sub(/.*com\//,"");sub(/\/.*/,"");print val,$0}' Input_file
Solution 2nd: Using while
loop here.
while IFS="/" read var1 var2 var3 var4 var5 var6
do
echo $var1 $var2 $var3 $var4 $var5 $var6 $var4
done < "Input_file"
If you are not satisfied with these all 4 solutions(including 2 before), kindly do let me know which approach you want to solve this, may be I could try to apply logic and get it done
Thanks,
R. Singh
1 Like
RudiC
September 13, 2017, 12:15pm
9
What IS "the other way round"?
bob123
September 13, 2017, 3:00pm
10
hi rav yes very satisfied all 4 work
thank you
as for the other way round i ment
take the last string in the line and insert it between 3rd and 4th backslash
from this
http://example.com// > test
to this
http://example.com/test/ > test
RudiC
September 13, 2017, 3:05pm
11
So the 4th field (between 3rd and 4th slash) is empty?
jgt
September 13, 2017, 3:49pm
12
cgc6:/tmp# echo "http://example.com/test/morestuff"|./t 999
test
http://example.com/test999/morestuff
cgc6:/tmp# cat t
IFS=/
while read a b c d e
do
echo $d
echo $a"/"$b"/"$c"/"$d$1"/"$e
done
cgc6:/tmp#
Simple and easy to maintain
1 Like
RudiC
September 13, 2017, 3:50pm
13
Would this come close to what you need?
echo "http://example.com// > test" | awk -F"/" '{$4 = $NF; sub (" > ",_, $4)}1' OFS="/"
http://example.com/test/ > test
1 Like
bob123
September 13, 2017, 5:46pm
14
rudic:
Would this come close to what you need?
echo "http://example.com// > test" | awk -F"/" '{$4 = $NF; sub (" > ",_, $4)}1' OFS="/"
http://example.com/test/ > test
works great
many thanks
awk -F"/" '{$4 = $NF; sub (" > ",_, $4)}1' OFS="/" file