awk script does not work when written as "one-liner"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script does not work when written as "one-liner"
# 1  
Old 07-11-2012
awk script does not work when written as "one-liner"

In my quest to solve a bigger problem (See my previous post called "Create SQL DML insert statements from file using AWK or similar" - sorry, not allowed to post urls until I have > 5 posts) I'm trying to get my head round awk, but have some problem figuring out why the following script does work while the corresponding(?) one-liner does not?

Here is the script that works:
Code:
#! /usr/bin/awk -f

/^ ABC_/{
 g++
 f=$1
}
{ 
  print $0>f
}

It splits a large file into smaller files based on blocks of text starting with " ABC_". And it does work.

However, If I try to enter this as a one-liner like this:
Code:
awk '/^ ABC_/{g++ f=$1} {print $0>f}' largefile.txt

I get the following error:
Code:
awk: line 1: syntax error at or near =

Why? What is wrong with my one-liner?
# 2  
Old 07-11-2012
missing semi colon after the g++

Code:
awk '/^ ABC_/{g++; f=$1} {print $0>f}' largefile.txt

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 07-11-2012
Quote:
Originally Posted by itkamaraj
missing semi colon after the g++
A missing semi colon it was - thank you very much itkamaraj!

---------- Post updated at 11:00 AM ---------- Previous update was at 09:42 AM ----------

Well, it worked fine on my Linux-box, but as soon as I moved it over to my really old Solaris server (Solaris 10 8/07) I get
Code:
awk: too many output files 10
 record number 55

So, based on info found elswhere on this forum, I tried to use nawk instead:
Code:
nawk '/^ ABC_/{g++; f=$1} {print $0>f; close(f)}' largefile.txt

But this gives me just empty files... Is there a workaround for my script when it comes to this limitation of 10 open files in Solaris? Can I tweak the script in some way?

---------- Post updated at 11:53 AM ---------- Previous update was at 11:00 AM ----------

I got past the number of open files limitation by using the POSIX compliant awk instead of the awk that is in the solaris standard path (/usr/bin/awk):
Code:
/usr/xpg4/bin/awk '/^ ABC_/{g++; f=$1} {print $0>f}' largefile.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Bash script: "mkdir -p" doesn't work with var(cat x)

Hello, :) I've an issue with the creation of a directory, All work without it :mad: So, below, my scripts with the debug output : #!/bin/bash # PATHS HOME_BACKUP="/home/backup" HOME_SCRIPT="/home/scripts/test/backup_server" TARGET="/var/www" # DATE DATE_Ymd=$(date +%Y-%m-%d) #... (1 Reply)
Discussion started by: Arnaudh78
1 Replies

3. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Simplify Bash Script Using "sed" Or "awk"

Input file: 2 aux003.net3.com error12 6 awn0117.net1.com error13 84 aux008 error14 29 aux001.ha.ux.isd.com error12 209 aux002.vm.ux.isd.com error34 21 alx0027.vm.net2.com error12 227 dux001.net5.com error123 22 us008.dot.net2.com error121 13 us009.net2.com error129Expected Output: 2... (4 Replies)
Discussion started by: sQew
4 Replies

6. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

7. Shell Programming and Scripting

awk's getline < "-" seems not work for pipe

Hi all, I have an gawk script to get user's input, So I use getline name < "-" (or getline name < "/dev/stdin") in my script They both work fine when my script deals with files. But it is broken for pipes. When I try "some command | my awk script", the variable name just gets an empty... (17 Replies)
Discussion started by: qiulang
17 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question