Replace substrings in awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace substrings in awk
# 1  
Old 01-18-2013
Replace substrings in awk

Hi !

my input looks like that:
Code:
--AAA-AAAAAAA---------AA-
AAA------AAAAAAAAAAAAAA
------A----AAAA-----A-------

Using awk, I would need to replace only the "-" located between the last letter and the end of the string by "~"

in order to get:
Code:
--AAA-AAAAAAA---------AA~
AAA------AAAAAAAAAAAAAA
------A----AAAA-----A~~~~~~~

I tried tio use gsub but it replace all the target "-" by only one "~" and doesn't conserve the number of replaced "-".

Thanks for your help ! Smilie
# 2  
Old 01-18-2013
Try:
Code:
awk '
{       if(match($0, /[-]+$/)) {
                t = substr($0, RSTART)
                gsub(/[-]/, "~", t)
                printf("%s%s\n", substr($0, 1, RSTART - 1), t)
        } else  print
}' input

If you are using a Solaris/Sun OS system, use /usr/xpg4/bin/awk or nawk, instead of awk.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 01-18-2013
Great ! Works perfectly, thanks Don Cragun !

---------- Post updated at 03:27 AM ---------- Previous update was at 02:40 AM ----------

I have another similar question.

input:
Code:
12345
123456789
1234567

How could I add n number of "~" at the end of the strings to have all the strings of the list with the same length (the longest string of the list being the "reference" string).

output:
Code:
12345~~~~
123456789
1234567~~

I tried something like that but the number of "-" being repeated doesn't work in the end section:
Code:
BEGIN{FS=OFS=""; max=0}

{
	if(NF>max){
		max=NF
	}
}

END{print $0 FS "~"{(max-NF)}

}
# 4  
Old 01-18-2013
Code:
echo "12345
123456789
1234" | awk '{printf "%-9s\n", $1}' | sed 's/ /-/g'

try this Smilie
# 5  
Old 01-18-2013
Try:
Code:
awk '
NR == 1{M = m = length($0)}
{       a[NR] = $0
        if(length($0) > M) M = length($0)
        if(length($0) < m) m = length($0)
}
END{    for(i = m; i <= M; i++) fill = fill "~"
        for(i = 1; i <= NR; i++)
                printf("%s%.*s\n", a[i], M - length(a[i]), fill)
}' input

Note also that using "" as the field separator is not portable. It does what you want on some systems, produces strange results on others, and gives errors on others.

Last edited by Don Cragun; 01-18-2013 at 05:02 AM.. Reason: Add note about using an empty string as a field separator.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 01-18-2013
Thanks for your help PikK45, unfortunately it doesn't work...

As I am learning awk I am trying to use a full awk command preferably

---------- Post updated at 04:05 AM ---------- Previous update was at 04:01 AM ----------

Thanks again Don Cragun, perfect !
# 7  
Old 01-18-2013
beca: that was a typing mistake

can you try using
Code:
sed 's/ /~/g'

instead of
Code:
sed 's/ /-/g'

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

2. Shell Programming and Scripting

Look for substrings with special characters

Hello gurus, I have a lookup table cat tmp1 \\\erw``~ 1 ^774574574565665f\] 2 ()42543^ and I`m trying to compare a bunch of strings such that, either the lookup table column 1, or the string to be looked up are substrings of each other (and return the second lookup column if yes). ... (2 Replies)
Discussion started by: sheetalk
2 Replies

3. Shell Programming and Scripting

Finding most common substrings

Hello, I would like to know what is the three most abundant substrings of length 6 from col2. The file is quite large and looks like this col1 col2 EN03 typehellobyedogcatcatdog EN09 typehellobyebyebyebye EN08 dogcatcatdogbyebyebyebye EN09 catcattypehellobyebyebyebye... (9 Replies)
Discussion started by: verse123
9 Replies

4. Shell Programming and Scripting

Extract three substrings from a logfile

I have a log file like below. 66.249.73.11 - - "UCiZ7QocVqYAABgwfP8AAHAA" "US" "Mediapartners-Google" "-" www.mahashwetha.com.sg "GET... (2 Replies)
Discussion started by: Tuxidow
2 Replies

5. Shell Programming and Scripting

extracting substrings from variables

Hello Everyone, I am looking for a way to extract substrings to local variables. Here is the format of the string variable i am using : /var/x/www && /usr/x/share/doc && /etc/x/logs where the substrings i must extract are the "/var/x/www" and such. I was originally thinking of using... (15 Replies)
Discussion started by: jimmy75_13
15 Replies

6. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

7. Shell Programming and Scripting

extracting substrings

Hi guys, I am stuck in this problem. Please help. I have two files. FILE1 (with records starting from '>' ) >TC1723_3 similar to Scific_A7Q9Q3 EMSPSQDYCDDYFKLTYPCTAGAQYYGRGALPVYWNYNYGAIGEALKLDLLNHPEYIEQN ATMAFQAAIWRWMNPMKKGQPSAHDAFVGNWKP >TC214_2 similar to Quiet_Ref100_Q8W2B2 Cluster;... (1 Reply)
Discussion started by: smriti_shridhar
1 Replies

8. AIX

Substrings and the likes in AIX 4.2 ?

In AIX 4.2, are there any shell commands to do substrings and the text like manipulation commands ? I want to take an error log where errors are multi-ligned and convert them into single lines to ease tracking/monitoring. I may need to shorten them out too. If I can manage to put them into an... (2 Replies)
Discussion started by: Browser_ice
2 Replies

9. Shell Programming and Scripting

Extract large list of substrings

I have a very long string (millions of characters). I have a file with start location and length that is thousands of rows long: Start Length 5 10 16 21 44 100 215 37 ... I'd like to extract the substring that corresponds to the start and length from each row of the list: I tried... (7 Replies)
Discussion started by: dcfargo
7 Replies

10. Shell Programming and Scripting

Breaking strings into Substrings

I'm only new to shell programming and have been given a task to do a program in .sh, however I've come to a point where I'm not sure what to do. This is my code so far: # process all arguments (i.e. loop while $1 is present) while ; do # echo "Arg is $1" case $1 in -h*|-H*) echo "help... (4 Replies)
Discussion started by: switch
4 Replies
Login or Register to Ask a Question