awk programming and sub function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk programming and sub function
# 1  
Old 09-15-2005
awk programming and sub function

I'm trying to use awk function sub or even gsub to search a file and replace all occurences of "$curYear|" with "$curYear ".

I need to remove the pipe delimator from only that field. I'm getting

awk: syntax error near line 1
awk: illegal statement near line 1

with this use in my ksh script

awk '{gsub(/$curYear|/, "$curYear ", $ofile)}'
# 2  
Old 09-15-2005
I am by no means an awk-expert, but I'll take a shot...

Did you try escaping the pipe-character by preciding it with a backslash? Like so:
Code:
awk '{gsub(/$curYear\|/, "$curYear ", $ofile)}'

# 3  
Old 09-15-2005
Nope

I just tried it but to no avail. I got the same error message.
# 4  
Old 09-15-2005
Code:
echo 'foo $curYear|bar|fred$curYear|zee bar1 bar2' | nawk '{gsub(/[$]curYear[|]/, "$curYear", $2); print}'

# 5  
Old 09-15-2005
Using the Ruby language, you can edit the file in place:

Code:
ruby -i.bak -pe'gsub(/\$curYear\|/, "$curYear ")' myfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. Shell Programming and Scripting

awk programming -Passing variable to awk for loop

Hi All, I am new to AWK programming. I have the following for loop in my awk program. cat printhtml.awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... (2 Replies)
Discussion started by: ctrld
2 Replies

3. Shell Programming and Scripting

awk programming

Need assistance using awk . Need assistance in awk programming. Any idea of getting the marked data into a file. </tr> <tr> <td class='labelOptional_ind'> cdr.00012325.0000000000000000.20130612.050005.WANP4722_csv </td> <td width='15%' class='labelOptional'> <div align='center'>... (8 Replies)
Discussion started by: ajayram_arya
8 Replies

4. Shell Programming and Scripting

AWK programming

Hi All, I read the AWK manual in the MAN page. But i didn't understand the below piece of code in the script TABLE=`echo "${FILE}" | awk -F"/" '{print $NF}' | cut -d"." -f1 | awk -F"_" '{print $NF}' 2>> ${LOGFILE}`; Please explain the above code. Thanks in advance ....... Regards,... (4 Replies)
Discussion started by: pdathu
4 Replies

5. Programming

awk programming

I have the list of numbers in a file 105.1 102.0 100.5 100 98 97.5 95 ... I want to get how many times I have numbers greater than a particular limit, say 100 in the list. How can I do that with awk command? (5 Replies)
Discussion started by: pranto_d
5 Replies

6. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

7. Programming

Please help! accept function problems in Socket programming

Hi, I have a client-server socket program. It has been working fine for over a year, but recently it started to show strange behavior.:confused: After the server program runs for a while, it will show in the top command saying it is using lots of CPU, MEM. I assume it means the server code is... (1 Reply)
Discussion started by: natxie
1 Replies

8. Programming

connect() function in C++ socket programming

Hello All, I have a problem using connect(...) function in C++. I am using SSH from my windows system to connect it to linux server. The program works fine if I run it directly in Linux machine but I need it to run through windows machine. The function returns -1 and so my program terminates. ... (3 Replies)
Discussion started by: smdhd3
3 Replies

9. Programming

setenv function in C programming

Hi I have a problem with setenv function in C. I'm calling an .exe of a c program from my shell script. Now, I have to pass a value from the .exe to my shell script. So i have exported an variable in my script. And in the C program I'm setting the variable as setenv("REC_CNT",rec_cnt,1); ... (7 Replies)
Discussion started by: janemary.a
7 Replies

10. Shell Programming and Scripting

awk programming

Hi all, i want to study harder awk programming. where can i get a good examples, problems and solutions. i'm in a hurry.. thanks, (5 Replies)
Discussion started by: tungaw2004
5 Replies
Login or Register to Ask a Question