Replace characters after match


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Replace characters after match
# 1  
Old 07-10-2019
Replace characters after match

I have a file with lines like:

Code:
foo find "https://localhost" "/website/foo" "this/that/the/other" "yes/no/maybe" -u admin -p admin > yes/no/maybe.txt

I'd like to replace the slashes in the last column with hyphens, like this:

Code:
foo find "https://localhost" "/website/foo" "this/that/the/other" "yes/no/maybe" -u admin -p admin > yes-no-maybe.txt

without affecting the other columns where I have slashes.

Last edited by RavinderSingh13; 07-11-2019 at 12:51 PM..
# 2  
Old 07-10-2019
Try (untested)

Code:
awk '{gsub ("/", "-", $NF)} 1' file

This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-10-2019
Quote:
Originally Posted by RudiC
Try (untested)

Code:
awk '{gsub ("/", "-", $NF)} 1' file

Thanks RudiC -- that works, someone else suggested:

awk '{gsub("/","-",$NF)}1'

the same it appears except for spaces. Both work.

Can you say what's going on here? Looks like number of fields NF and delimiters are being used along with gsub? (what's that), how is this applying only to the last column?
# 4  
Old 07-10-2019
NF number of fields
$1 first field
$2 field 2
$NF last field
$(NF-1) second last field
sub() substitute
gsub() globally substitute, apply multiple subsequent substitutions
sub("/", "-", $NF) substitute a / with a - in the last field
gsub("/", "-", $NF) globally substitute a / with a - in the last field
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 07-10-2019
$NF is the last column in any record read from a file. It is an awk built-in variable. There are several of them. Since you want the last column changed you need to replace the / character with the - character. gsub, an awk builtin function, means substitute a pattern (1 or more characters). Use characters a new set of character(s) the replacement.


So using gsub on $NF does what you asked for.

See: AWK Built-in Variables
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Inserting n characters to beginning of line if match

I would like to insert n number of characters at the beginning of each line that starts with a given character. If possible, I would be most appreciative for a sed or awk solution. Given the data below, I would like to be able to insert either 125 spaces or 125 "-" at the beginning of every line... (6 Replies)
Discussion started by: jvoot
6 Replies

2. Shell Programming and Scripting

awk match shell variable that contains special characters?

How to match a shell variable that contains parenthesis (and other special characters like "!") file.txt contains: Charles Dickens Matthew Lewis (writer) name="Matthew Lewis (writer)"; awk -v na="$name" ' $0 ~ na' file.txt Ideally this would match $name in file.txt (in this... (3 Replies)
Discussion started by: Mid Ocean
3 Replies

3. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

4. Shell Programming and Scripting

How to extract next n characters after a match is found?

Hi, I want to extract the next 7 characters after I encounter the first ( in the code eg abc123=(xvn1342) xyz678123=(ret8901) I want to extract xvn1342,ret8901. Please advise how to achieve this with awk, if possible? (9 Replies)
Discussion started by: sidnow
9 Replies

5. UNIX for Dummies Questions & Answers

Display/Cut the characters based on match

I have input file like this update tablename set column1='ABC',column2='BBC' where columnx=1 and columny=100 and columnz='10000001' update tablename set column1='ABC',column2='BBC',column3='CBC' where columnx=1 and columny=100 and columnz='10000002' update tablename set column1='ABC' where... (1 Reply)
Discussion started by: nsuresh316
1 Replies

6. Shell Programming and Scripting

Find out match characters on all lines

I have a file with 22 lines. Each line has only 5 different chars, no white space, and each line is 3,278,824 in length. The 5 chars is "-", "A", "B", "C", "D". Below is an example of the first 25 chars of the first four lines of the file. -----ABCDA--CD-BBBBB----D --A--ABCD--DCD-BBBBC-----... (12 Replies)
Discussion started by: cwzkevin
12 Replies

7. Shell Programming and Scripting

Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below. test!=123-> test\!\=123 !@#$%^&*()-= to be replaced by \!\@\#\$\%\^\&\*\(\)\-\= (8 Replies)
Discussion started by: laknar
8 Replies

8. Shell Programming and Scripting

search pattern and replace x-y characters in nth line after every match

Hi, I am looking for any script which can do the following. have to read a pattern from fileA and copy it to fileB. fileA: ... ... Header ... ... ..p1 ... ... fileB: .... .... Header (3 Replies)
Discussion started by: anilvk
3 Replies

9. Shell Programming and Scripting

How to replace characters with random characters

I've got a file (numbers.txt) filled with numbers and I want to replace each one of those numbers with a new random number between 0 and 9. This is my script so far: #!/bin/bash rand=$(($RANDOM % 9)) sed -i s//$rand/g numbers.txtThe problem that I have is that it replaces each number with just... (2 Replies)
Discussion started by: hellocatfood
2 Replies

10. Shell Programming and Scripting

Regular Expression to match repeated characters

Hello All I have file which contain sample data like below - test.txt ---------------------------------------------- jambesh aaa india trxxx sdasd mentor asss light train bbblah --------------------------------------------- I want to write a regX which would print only those... (4 Replies)
Discussion started by: jambesh
4 Replies
Login or Register to Ask a Question