Skip to content

Commit d4814d2

Browse files
examples for creating new rows/columns
1 parent 228e61f commit d4814d2

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

gnu_awk.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* [Two file processing](#two-file-processing)
2828
* [Comparing whole lines](#comparing-whole-lines)
2929
* [Comparing specific fields](#comparing-specific-fields)
30+
* [Creating new fields](#creating-new-fields)
3031
* [Dealing with duplicates](#dealing-with-duplicates)
3132
* [Lines between two REGEXPs](#lines-between-two-regexps)
3233
* [All unbroken blocks](#all-unbroken-blocks)
@@ -1409,6 +1410,58 @@ ECE Om 92
14091410

14101411
<br>
14111412

1413+
## <a name="creating-new-fields"></a>Creating new fields
1414+
1415+
* Number of fields in input record can be changes by simply manipulating `NF`
1416+
1417+
```bash
1418+
$ # reducing fields
1419+
$ echo 'foo,bar,123,baz' | awk -F, -v OFS=, '{NF=2} 1'
1420+
foo,bar
1421+
1422+
$ # creating new empty field(s)
1423+
$ echo 'foo,bar,123,baz' | awk -F, -v OFS=, '{NF=5} 1'
1424+
foo,bar,123,baz,
1425+
1426+
$ # assigning to field greater than NF will create empty fields as needed
1427+
$ echo 'foo,bar,123,baz' | awk -F, -v OFS=, '{$7=42} 1'
1428+
foo,bar,123,baz,,,42
1429+
1430+
$ # adding a new 'Grade' field
1431+
$ awk 'BEGIN{OFS="\t"; g[9]="S"; g[8]="A"; g[7]="B"; g[6]="C"; g[5]="D"}
1432+
{NF++; if(NR==1)$NF="Grade"; else $NF=g[int($(NF-1)/10)]} 1' marks.txt
1433+
Dept Name Marks Grade
1434+
ECE Raj 53 D
1435+
ECE Joel 72 B
1436+
EEE Moi 68 C
1437+
CSE Surya 81 A
1438+
EEE Tia 59 D
1439+
ECE Om 92 S
1440+
CSE Amy 67 C
1441+
```
1442+
1443+
* two file example
1444+
1445+
```bash
1446+
$ cat list4
1447+
Raj class_rep
1448+
Amy sports_rep
1449+
Tia placement_rep
1450+
1451+
$ awk -v OFS='\t' 'NR==FNR{r[$1]=$2; next}
1452+
{NF++; if(FNR==1)$NF="Role"; else $NF=r[$2]} 1' list4 marks.txt
1453+
Dept Name Marks Role
1454+
ECE Raj 53 class_rep
1455+
ECE Joel 72
1456+
EEE Moi 68
1457+
CSE Surya 81
1458+
EEE Tia 59 placement_rep
1459+
ECE Om 92
1460+
CSE Amy 67 sports_rep
1461+
```
1462+
1463+
<br>
1464+
14121465
## <a name="dealing-with-duplicates"></a>Dealing with duplicates
14131466

14141467
* default value of uninitialized variable is `0` in numeric context and empty string in text context
@@ -1850,6 +1903,7 @@ $ printf 'hi👍' | awk '{print length()}'
18501903
* `split` function - similar to `FS` splitting input record into fields
18511904
* use `patsplit` function to get results similar to `FPAT`
18521905
* See also [gawk manual - Split function](https://www.gnu.org/software/gawk/manual/gawk.html#index-split_0028_0029-function)
1906+
* See also [unix.stackexchange - delimit second column](https://unix.stackexchange.com/questions/372253/awk-command-to-delimit-the-second-column)
18531907
18541908
```bash
18551909
$ # 1st argument is string to be split
@@ -1872,6 +1926,13 @@ $ echo "$s" | awk '{n=split($0,s,/[0-9]+/,seps); for(i=1;i<n;i++)print seps[i]}'
18721926
123
18731927
54
18741928
908
1929+
1930+
$ # single row to multiple rows based on splitting last field
1931+
$ s='foo,baz,12:42:3'
1932+
$ echo "$s" | awk -F, '{n=split($NF,a,":"); NF--; for(i=1;i<=n;i++) print $0,a[i]}'
1933+
foo baz 12
1934+
foo baz 42
1935+
foo baz 3
18751936
```
18761937
18771938
* `substr` function allows to extract specified number of characters from given string

0 commit comments

Comments
 (0)