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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to print the string between 3rd and 4th backslashs to end of line
# 8  
Old 09-13-2017
Quote:
Originally Posted by bob123
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.
Code:
awk '{val=$0;sub(/.*com\//,"");sub(/\/.*/,"");print val,$0}'  Input_file

Solution 2nd: Using while loop here.
Code:
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 Smilie

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 9  
Old 09-13-2017
What IS "the other way round"?
# 10  
Old 09-13-2017
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

Code:
http://example.com// > test

to this


Code:
http://example.com/test/ > test

# 11  
Old 09-13-2017
So the 4th field (between 3rd and 4th slash) is empty?
# 12  
Old 09-13-2017
Code:
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
This User Gave Thanks to jgt For This Post:
# 13  
Old 09-13-2017
Would this come close to what you need?
Code:
echo "http://example.com// > test" | awk -F"/" '{$4 = $NF; sub (" > ",_, $4)}1' OFS="/"
http://example.com/test/ > test

This User Gave Thanks to RudiC For This Post:
# 14  
Old 09-13-2017
Quote:
Originally Posted by RudiC
Would this come close to what you need?
Code:
echo "http://example.com// > test" | awk -F"/" '{$4 = $NF; sub (" > ",_, $4)}1' OFS="/"
http://example.com/test/ > test

works great
many thanks
Code:
awk -F"/" '{$4 = $NF; sub (" > ",_, $4)}1' OFS="/" file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

UNIX help to print 50 lines after every 3rd occurrence pattern till end of file

I need help with extract/print lines till stop pattern. This needs to happen after every 3rd occurrence of start pattern and continue till end of file. Consider below is an example of the log file. my start pattern will be every 3rd occurrence of ERROR_FILE_NOT_FOUND and stop pattern will be... (5 Replies)
Discussion started by: NSS
5 Replies

3. Shell Programming and Scripting

awk one liner to print to end of line

Given: 1,2,whatever,a,940,sot how can i print from one particular field to the end of line? awk -F"," '{print $2 - endofline}' the delimiter just happens to be a comma "," in this case. in other cases, it could be hypens: 1---2---whatever---a---940---sot (4 Replies)
Discussion started by: SkySmart
4 Replies

4. Shell Programming and Scripting

1st column,2nd column on first line 3rd,4th on second line ect...

I need to take one column of data and put it into the following format: 1st line,2nd line 3rd line,4th line 5th line,6th line ... Thanks! (6 Replies)
Discussion started by: batcho
6 Replies

5. Shell Programming and Scripting

strange: sed and awk print at end instead of begin of line

Hi! I have a strange behaviour from sed and awk, but I'm not sure, if I'm doing something wrong: I have a list of words, where I want to add the following string at the end of each line: \;\;\;\;0\;1 I try like this: $ cat myfile | awk '{if ( $0 != "" ) print $0"\;\;\;\;0\;1"}' Result:... (5 Replies)
Discussion started by: regisl67
5 Replies

6. Shell Programming and Scripting

awk print last line returns empty string

hello I have a file with lines of info separated with "|" I want to amend the second field of the last line, using AWK my problem is with geting awk to return the last line this is what I am using awk 'END{ print $0 }' myFile but I get an empty result I tried the... (13 Replies)
Discussion started by: TasosARISFC
13 Replies

7. Shell Programming and Scripting

awk print second line after search string

I have multiple config files where I need to pull the ip address from loopback3. The format is the same in every file, the ip is the second line after interface loopback3. interface loopback2 loopback description router ID ip address 192.168.1.1 interface loopback3 loopback description... (3 Replies)
Discussion started by: numele
3 Replies

8. Shell Programming and Scripting

How to extract 3rd line 4th column of a file

Hi, Shell script: I would need help on How to extract 3rd line 4th column of a file with single liner Thanks in advance. (4 Replies)
Discussion started by: krishnamurthig
4 Replies

9. Shell Programming and Scripting

Print starting 3rd line until end of the file.

Hi, I want to Print starting 3rd line until end of the file. Pls let me know the command. Thanks in advance. (1 Reply)
Discussion started by: smc3
1 Replies

10. Shell Programming and Scripting

printing 3rd or 4th feild from last in awk.

Whats up fellas... hope someone can help me with the following... I am parsing an file that is space delimited, however, in the middle, there is an ugly "Account Name" feild that in itself has multiple varying spaces, and commas which throws off my script. The 1st 3 feilds I am able to obtain... (8 Replies)
Discussion started by: djsal
8 Replies
Login or Register to Ask a Question