Sponsored Content
Top Forums UNIX for Dummies Questions & Answers awk construct unfamiliar to me Post 302678557 by agama on Saturday 28th of July 2012 10:42:43 AM
Old 07-28-2012
Awk's basic syntax is <expression> {<code-block>}. If expression evaluates to true, the associated block is executed. Either may be omitted; if the expression is omitted, true is assumed, and if the code block is omitted "print" is assumed. 1 evaluates as the expression to true, and so the single 1 imples 1 { print }.

---------- Post updated at 10:42 ---------- Previous update was at 10:37 ----------

Looking at the example you gave, this is how I would have written it:

Code:
awk '{for(i=2;i<=NF;i++)if($i==$1)$i=RS $i; print;}' input-file

It might be easier to see the difference if not smashed all on one line:

Code:
awk '
   {
      for( i=2; i<=NF; i++ )
         if( $i == $1)
            $i = RS $i; 
      print;
   }' input-file


Last edited by agama; 07-28-2012 at 11:44 AM.. Reason: typo
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with looping construct

Hi all I have tried to search for this, but keep getting a MySQL db connect error, so am posing the question here, and taking a risk of incurring the wrath of the mods with my first post... I have the following test script: #!/bin/bash HTTPD=`/bin/ps -axcu | /usr/bin/grep httpd... (6 Replies)
Discussion started by: mikie
6 Replies

2. Shell Programming and Scripting

ksh construct

Hi Guys, could someone tell me what this ksh construct does typeset -r PROG_PWD=${0%/*} does I understand the -r for readonly but I would very much appreciate a definitive account of what this will set $PROG_PWD to. If I run this at the cmd line it it gets set to /usr/bin but I would... (2 Replies)
Discussion started by: ajcannon
2 Replies

3. UNIX for Advanced & Expert Users

unfamiliar errors

I was checking my error logs today and ran into some errors I have not seen before maybe somebody has run into these before? /etc/cron.quarter-hourly/owusers.sh: Set effective gid to mail(12) failed! /usr/local/mailwatch/check_sendmail_relay.sh: line 8: thanks (1 Reply)
Discussion started by: mcraul
1 Replies

4. Shell Programming and Scripting

if-else construct not working

Hi all, Sorry to ask this easy question but I am stuck. In a scenario i am executing one shell script which contains a if - else construct : if ; then echo $line $line >> successful_build.txt else $line >> failed_services.txt fi explaination : if the... (5 Replies)
Discussion started by: bhaskar_m
5 Replies

5. Shell Programming and Scripting

syntax error in the if construct

Hi can anyone tell me why is this code giving error mode=$1 if ] || ] then echo "MODES:" exit 1 fi Thanks (5 Replies)
Discussion started by: Anteus
5 Replies

6. Shell Programming and Scripting

Unfamiliar array syntax

I recently was handed responsibility for a script which has array references that are new to me and I could use some help understanding them. Parts of the config file and script are included. This script collects backups from a jail server of the jails running on that host. Several jail... (1 Reply)
Discussion started by: thumper
1 Replies

7. Shell Programming and Scripting

Help with if-else construct

Hi all i have been trying to do a small 'question and answer' script using if-else statement and a combination of pipe. I have succeeded in allowing the user to login with user name and password stored in a sequence username/password in a file named "pass" like this: echo "please enter your... (14 Replies)
Discussion started by: arikutex
14 Replies

8. Shell Programming and Scripting

Construct path

Hi, I need to construct the below path from the two available directory path, O/P /home/data/test/run/ht/WEB/HTML /home/data/test/run/ht/WEB/JSP /home/data/test/run/ht/WEB/CSS Path:1 ------ /home/data/test/run/ Path:2 ------ /home/data/share/app/01/lang/ht/WEB/HTML... (5 Replies)
Discussion started by: vel4ever
5 Replies

9. Shell Programming and Scripting

How would I construct a (possibly simple) IF statement?

Hi all, I thought this would be simple, but I've been having a lot of trouble trying to write this IF statement, if I may ask for help pls: In BASH, how would I construct the if statement: Should ONLY be true if USEROPTscript=="yes"]] AND $mode=="INSTALL" /or/ $mode=="CHANGE" ]]... (3 Replies)
Discussion started by: jmccoughlin
3 Replies
ucblinks(1B)					     SunOS/BSD Compatibility Package Commands					      ucblinks(1B)

NAME
ucblinks - adds /dev entries to give SunOS 4.x compatible names to SunOS 5.x devices SYNOPSIS
/usr/ucb/ucblinks [-e rulebase] [-r rootdir] DESCRIPTION
ucblinks creates symbolic links under the /dev directory for devices whose SunOS 5.x names differ from their SunOS 4.x names. Where possi- ble, these symbolic links point to the device's SunOS 5.x name rather than to the actual /devices entry. ucblinks does not remove unneeded compatibility links; these must be removed by hand. ucblinks should be called each time the system is reconfiguration-booted, after any new SunOS 5.x links that are needed have been created, since the reconfiguration may have resulted in more compatibility names being needed. In releases prior to SunOS 5.4, ucblinks used a nawk rule-base to construct the SunOS 4.x compatible names. ucblinks no longer uses nawk for the default operation, although nawk rule-bases can still be specifed with the -e option. The nawk rule-base equivalent to the SunOS 5.4 default operation can be found in /usr/ucblib/ucblinks.awk. OPTIONS
-e rulebase Specify rulebase as the file containing nawk(1) pattern-action statements. -r rootdir Specify rootdir as the directory under which dev and devices will be found, rather than the standard root directory /. FILES
/usr/ucblib/ucblinks.awk sample rule-base for compatibility links ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWscpu | +-----------------------------+-----------------------------+ SEE ALSO
devlinks(1M), disks(1M), ports(1M), tapes(1M), attributes(5) SunOS 5.10 13 Apr 1994 ucblinks(1B)
All times are GMT -4. The time now is 04:46 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy