awk to print the string between 3rd and 4th backslashs to end of line

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

Any attempts / ideas / thoughts from your side?

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

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

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 :slight_smile:

Thanks,
R. Singh

1 Like

What IS "the other way round"?

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

So the 4th field (between 3rd and 4th slash) is empty?

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

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

works great
many thanks

awk -F"/" '{$4 = $NF; sub (" > ",_, $4)}1' OFS="/" file