Replace character in awk array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace character in awk array
# 1  
Old 03-22-2017
Replace character in awk array

Code:
somedata | awk -F"#soils#" '{split($1,a,"NITNUM=");  print a[2]}'

how can i edit the content of array 2 above?

what i want to do is get rid of single quotes and double quotes. and then turn the "NewLine" into new lines.

the data in array 2 (a[2]) is:

Code:
"'327685650858'NewLine'4048854814312743'"NewLine1089901687312447NewLine1089901687304767NewLine6669407900051008NewLine3786114055313675

the expected result after the above is modiied should be:

Code:
327685650858
4048854814312743
1089901687312447
1089901687304767
6669407900051008
3786114055313675

here's what im currently doing to obtain the desire results:

Code:
somedata | awk -F"#soils#" '{split($1,a,"NITNUM=");  print a[2]}' | sed -e 's_"__g' -e 's_ __g' -e '/^$/d' 2>/dev/null | awk '{gsub("NewLine","\n");printf"%s",$0}' | sed -e "s/'/ /g" -e 's~ ~~g' | sed '/^$/d'

im hoping to find a portable solution for this. i cant justify running this many commands when i can just do it all with awk.
# 2  
Old 03-22-2017
Hello SkySmart,

So for your array a's a[2] value I have taken as a line and tried following.
Code:
echo -e \"\'327685650858\'NewLine\'4048854814312743\'\"NewLine1089901687312447NewLine1089901687304767NewLine6669407900051008NewLine3786114055313675 | awk -vs1="'" '{gsub(/\"/,"",$0);gsub(s1,"",$0);num=split($0, a,"NewLine");for(i=1;i<=num;i++){print a[i]}}'

For your code you could try to add this one into your existing code. Add -vs1="'" into your code.
Code:
'{gsub(/\"/,"",a[2]);gsub(s1,"",a[2]);num=split(a[2], array,"NewLine");for(i=1;i<=num;i++){print array[i]}}'

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 03-22-2017
Try
Code:
{gsub ("\047|\"", _, a[2]); gsub ("NewLine", "\n", a[2]); print a[2]}

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed inside the awk script to replace a string in the array

The requirement is i need to find an array value matching with pattern {5:{ , replace that with 5: and reassign that to same array index and print it. I write something like below and the issue is sed command is not working. If i replace " with "`" the script gives syntax error.how can i... (8 Replies)
Discussion started by: bhagya123
8 Replies

2. UNIX for Dummies Questions & Answers

Find and replace single character w/awk given conditions

I have a file that looks like this: 14985 DPN verb PPa to spend. 12886 DPNDJN bay tree. 15686 DQ verb to observe 15656 KC ... (7 Replies)
Discussion started by: jvoot
7 Replies

3. UNIX for Advanced & Expert Users

Replace certain character at specific place with related character

hello i have file with 100k records and each one has certain value that starts at 28th column and certain value that starts at 88th column e.g. 1st file <25>1234567 ..... <88> 8573785485 i have aditional file with values which are related to value that starts at 88th column of the... (1 Reply)
Discussion started by: dell1520
1 Replies

4. Shell Programming and Scripting

awk script to replace nth character with comma

I have a requirement as below. In one of my column, I have data which may or may not be separted with coma always. Now I need to validate the length of these text within the coma (if available) and if the length is more than 30 characters, I need to insert a coma either at 30 characters if its... (3 Replies)
Discussion started by: aramacha
3 Replies

5. Programming

Character pointer to Character array

how to copy content of character pointer to character array in c programming.. char *num; char name=num; (1 Reply)
Discussion started by: zinat
1 Replies

6. Shell Programming and Scripting

Replace multiple occurances of same character with a single character.

Hi all, Greetings, I have the following scenario, The contents of main file are like : Unix|||||forum|||||||||||||||is||||||the||best so||||||be|||||on||||||||||||||||||||||||||||||||||||||||||||it And i need the output in the following form: Unix=forum=is=the=best so=be=on=it ... (3 Replies)
Discussion started by: dipanchandra
3 Replies

7. Shell Programming and Scripting

In Sed how can I replace starting from the 7th character to the 15th character.

Hi All, Was wondering how I can do the following.... I have a String as follows "ACCTRL000005022RRWDKKEEDKDD...." This string can be in a file called tail.out or in a Variable called $VAR2 Now I have another variable called $VAR1="000004785" (9 bytes long), I need the content of... (5 Replies)
Discussion started by: mohullah
5 Replies

8. 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

9. Shell Programming and Scripting

AWK: replace single positional character given variables

I already have accomplished this task using sed and arrays, but since I get the variable using awk, I figured I'd ask this question and maybe I can get a cleaner solution using strictly awk.. I just can't quite grasp it in awk. Story: I'm automating the (re)configuration of network interfaces,... (3 Replies)
Discussion started by: System Shock
3 Replies

10. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies
Login or Register to Ask a Question