Tag Info

Hot answers tagged

13

You can make smart use of the NF variable in awk awk '{print $NF}' File1 From man awk NF The number of fields in the current input record. So NF will give you the amount of fields and $NF will then expand to $3 for example, which you can use in a print statement.


13

No. The for(i=0;i<10;i++) is a classic programming construct (see Traditional for loops) that is present in many languages. It can be broken down to: start-expression; end-condition; end-of-iteration-expression In other words, what I wrote above means "initialize i to 0 and, while i is less than 10, do something and then increment i by 1. Yes the ...


13

Your redirections have a race condition. This: >(wc -l | awk '{print $1}' > n.txt) runs in parallel with: awk 'BEGIN{getline n < "n.txt"}...' later in the pipeline. Sometimes, n.txt is still empty when the awk program starts running. This is (obliquely) documented in the Bash Reference Manual. In a pipeline: The output of each command in ...


9

One way of doing it using GNU awk is this: echo 20140805234656 | awk 'BEGIN { FIELDWIDTHS = "4 2 2 2 2 2" } { printf "%s-%s-%s %s:%s:%s\n", $1, $2, $3, $4, $5, $6 }'


7

You can for example use this: $ awk '/H/{sub("H", "H"++v)}1' file 1562 first part 1563 H1 col3 H col4 1564 H2 col3 H col4 3241 H3 col3 H col4 3242 third part ... This looks for those lines containing H and replaces that H with H together with a variable we keep incrementing. Note you could use gsub() instead of sub() if you wanted to perform ...


6

You have: $NF=a[FNR] as the final condition (the one that determines whether to print). Assignments return the value assigned, in this case a[FNR]. The first line of the data file from the linked question is: A 0 a[FNR] is initialised to $2. That means the value of a[FNR] is 0, which is a false value to awk. That means the assignment is false, which ...


6

You should use -v option of awk: awk -F',' -v accNum="$1" '$1 == accNum {print $3,$2}' With $1 in double quote, the shell will handle all its special character ( if has ) for you.


6

Sed can't do arithmetic┬╣. Use awk instead. awk ' $4 == "calc" {sub(/calc( |\t)/, sprintf("%-6.2f", $3 - $2))} 1' The 1 at the end means to print everything (after any preceding transformation). Instead of the text substitution with sub, you could assign to $4, but doing so replaces inter-column space (which can be any sequence of spaces and tabs) ...


6

You must use regex ^ to denote start of string: $ awk '$2 ~ /^[[:upper:]]/' file ID A56 DS AGE 56


6

The syntax delete array is not in current versions in POSIX, but it is supported by virtually all existing implementations (including the original awk, GNU, mawk, and BusyBox). It will be added in a future version of POSIX (see defect 0000544). An alternate way to clear all array elements, which is both portable and standard-compliant, and which is an ...


6

what about: awk '{printf "%-25s %s\n",$2,$1}' file See: cat file 123 OneTwoThree 234 TwoThreeFour 345 ThreeFourFive 789 abcdefghijklmnopqrstuvwxyz 999 abcdefghijklmnopqrstuvwxyzABC Output: awk '{printf "%-25s %s\n",$2,$1}' file OneTwoThree 123 TwoThreeFour 234 ThreeFourFive 345 abcdefghijklmnopqrstuvwxyz 789 ...


6

Since terdon provided a comprehensive answer I just want to add that if any column evaluates to false, the for statement ends the loop, as you can see in this example: $ echo 1 2 3 4 5 0 6|awk '{for(x=1;$x;++x)print $x}' 1 2 3 4 5


6

tac filename |awk '/man1/ { print $1,$2} '| awk '!x[$0]++' | tac Testing I wanted to test with more concrete input. So, my testing is as below. cat filename blah blah blah blah blahblah man1 boy1 blah blah man1 boy2 man1 boy1 man1 boy2 man1 boy3 man1 boy4 man1 boy2 Now, I run the above command and get the output as, tac filename |awk '/man1/ { print ...


6

You can use substring as follow: echo 20140805234656 | awk '{print substr($0,1,4)"-"substr($0,5,2)"-"substr($0,7,2)" "substr($0,9,2)":"substr($0,11,2)":"substr($0,13,2) }' Probably there are easier ways too.


5

An ugly way of doing this (i.e. causing a function call in shell based on output from awk) could look like this: awk -F '\t' ' FNR < 2 {next} FNR == NR { for (i=2; i <= NF; i++) { if (($i == 1) || ($i == 4)) printf "retrieve %s\n", $i if (($i == 2) || ($i == 2)) printf "retrieve2 ...


5

awk '$1 ~ /^E|^F/ {if ($1 == "End") print $1" "$2; if ($1 == "Fin") print $1" "$3}' or awk '/^End/{print $1" "$2}/^Fin/{print $1" "$3}' (thanks to Jidder) Should work.


5

You could use awk as @Gnouc suggested, or GNU grep grep -P '^[^\s]+\s+[A-Z]' file Perl perl -lane 'print if $F[1]=~/^[A-Z]/' file GNU sed sed -rn '/^[^\s]+\s+[A-Z]/p' file shell (assumes a recent version of ksh93, zsh or bash) while read -r a b; do [[ $b =~ ^[A-Z] ]] && printf "%s %s\n" "$a" "$b"; done < file


5

This looks like a case where cut will do better than awk: cut -d , -f1-20 Precipitation.csv > aaa1 cut -d , -f21-40 Precipitation.csv > aaa2 cut -d , -f41-100 Precipitation.csv > aaa3 -d , specifies the delimiter (a comma, since the input is called CSV, but you can change that). -f N-M picks out fields N through M inclusive to be in the output. ...


5

A sed one: sed ' s/[^[:blank:]]\{1,\}/,&,/g;:1 s/\(\(,[^,[:blank:]]*\)\(,[^,[:blank:]]*\)*\)\2,/\1,/;t1 s/,\([^[:blank:]]*\),/\1/g' (it processes all the fields that contain , characters and preserves spacing)


5

The documentation that you're referring to is of the GNU version of Awk, but the version you've is mawk (as shown by your first command) which is an awk variant that doesn't seem to support POSIX character classes like [:alpha:] or [:alnum:]. Edit: As mentioned by Gnouc, mawk does support POSIX character classes from version 1.3.4 onwards, so an update ...


5

You can use sed: sed -e 's/[0-9]*=/=/' < data This replaces (s) any text that is zero or more characters in the range 0-9 followed by an = sign with just the = sign.


5

pipe expression in process substitution causes a race condition in bash and ksh, zsh doesn't. The main problem here is that zsh waits, bash doesn't. You can see more details here. A quick fixed, adding sleep 1 in your awk to make n.txt always available: awk 'BEGIN{system("sleep 1");getline n < "n.txt"}{print $1 "\t" $1/n*100 "\t" $2}'


5

First of all, you should use a proper CSV parser. For example, in Perl, you can use Text::CSV: Install cpanm (if you use Perl, you'll thank me later) $ sudo apt-get install cpanminus If you're not on a Debian based system, you should be able to install it using your distribution's package manager. Install the Text::CSV module $ sudo cpanm Text::CSV ...


4

A perl solution: $ perl -ple 's/\\\.1\\\.(7|8|9)/\\.1\\.10/' file | uniq RewiteEngine On RewriteCond %{REMOTE_ADDR} !^192\.168\.1\.10$ [NC] RewriteCond %{REMOTE_ADDR} ^192\.168\.1\.5$ [NC] </Directory> If you want edit inplace, you can try: perl -i.bak -nle 'next if $count and /!\^/;s/\\\.1\\\.(7|8|9)/\\.1\\.10/ and $count++ if ...


4

process_data() { awk -F /dev/fd/3 3<< \EOF awk code here EOF } Note that command line arguments can contain newline character, and while there's a length limit, it's general over a few hundred kilobyte. awk ' BEGIN {...} /.../ ... END {...} ' If the issue is about embedding single quote characters in the awk script, another approach is ...


4

It's a bug in mawk 1.3.3 and was reported here. You can upgrade to mawk 1.3.4 or use patch to fix the bug. $ mawk -W version mawk 1.3.4 20130219 Copyright 2013, Thomas E. Dickey Copyright 1996, Michael D. Brennan internal regex compiled limits: max NF 32767 sprintf buffer 2040 $ echo "host.company.com has address 192.168.22.82" | mawk ...


4

awk -F: '{if (NR>1 && save!=$1) print "";} {save=$1; print;}' You never want to insert a blank line before line 1, so don't even think about it unless NR>1.  Thereafter, print the blank line if the first field is not the saved value from the previous line.


4

Per the gawk manual, which is a good general awk language reference: An important aspect to remember about arrays is that array subscripts are always strings. That is, awk arrays are always associative, and numeric keys are stringified. Only the keys that are in use are stored in the array (and maybe some extra space for the future). Numeric indices ...


4

you can do this using this shell script: #!/bin/bash awk '/man1/{pos[$0] = NR} END { for(key in pos) reverse[pos[key]] = key for(nr=1;nr<=NR;nr++) if(nr in reverse) print reverse[nr] }' yourfile Output: [root@host ~]# sh shell.sh man1 boy1 man1 boy3 man1 boy4 man1 boy2 Source


4

With zsh: $ printf '%s\n' ${(Oau)${(MOa)${(f)"$(<file)"}:#man1*}} man1 boy1 man1 boy3 man1 boy4 man1 boy2 Those are parameter expansion flags: f: split on newline ${(M)array:#pattern}: expands to the elements matching the pattern Oa: reverse the order of array u: unique



Only top voted, non community-wiki answers of a minimum length are eligible