Skip to content

Commit

Permalink
post: update an awk example
Browse files Browse the repository at this point in the history
  • Loading branch information
at-ishikawa committed Jul 5, 2024
1 parent affe272 commit 1aca3ac
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions _posts/2024-03-30-linux-command-cheetsheet.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
---
title: Linux command cheetsheet
date: 2024-03-30
last_modified_at: 2024-07-05
tags:
- linux
- awk
- sed
- grep
---

# Basic commands
Expand Down Expand Up @@ -32,6 +36,14 @@ tags:
- `$LINE_NUM_FROM,$LINE_NUM_TO`: Select lines from `$LINE_NUM_FROM` to `$LINE_NUM_TO`

## awk

- `-F` is field separator. The default value is a `0x20`, which matches a space, tab, and newlines [this article](https://stackoverflow.com/a/30406868).
- There can be some generic syntaxes like
- Variable can be defined and used
- Internal variables: `{ var=$1; print var }`
- Conditional flow like `if (condition1) { statement1; }`
- `next` can skip a line

## xargs
## envsubst

Expand Down Expand Up @@ -108,3 +120,23 @@ aa
ab
a
```

## How to output the 1st and 3rd column from a CSV file

Using `awk`

```bash
> echo -e 'aa,ab,ac\nba,bb,bc\nca,cb,cc' | awk -F ',' '{ print $1,":",$3 }'
aa : ac
ba : bc
ca : cc
```

Using `cut`

```bash
> echo -e 'aa,ab,ac\nba,bb,bc\nca,cb,cc' | cut -d "," -f 1,3
aa,ac
ba,bc
ca,cc
```

0 comments on commit 1aca3ac

Please sign in to comment.