https://github.com/mkayontour/regex_sed_awk_magix
Just a bunch of notes for myself
https://github.com/mkayontour/regex_sed_awk_magix
awk magic notes regex sed
Last synced: 3 months ago
JSON representation
Just a bunch of notes for myself
- Host: GitHub
- URL: https://github.com/mkayontour/regex_sed_awk_magix
- Owner: mkayontour
- Created: 2018-01-13T11:33:46.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-13T12:17:26.000Z (over 8 years ago)
- Last Synced: 2025-03-05T21:43:20.137Z (over 1 year ago)
- Topics: awk, magic, notes, regex, sed
- Language: Shell
- Homepage:
- Size: 3.91 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## AWK
#### Multiline
Do some multiline magic and bring those into one line.
```
cat multiline_file
--
name ich
info langsam
platz 2
--
name du
info schnell
platz 1
```
```
awk '/--/{if (x)print x;x="";next}{x=(!x)?$0:x" "$0;}END{print x;}' multiline_file
```
Result:
```
name ich info langsam platz 2
name du info schnell platz 1
```
### gsub
#### Whitespaces and Tabs
Search and replace with gsub in awk
Text with tabs or two whitespaces, will be replaced by `;`
```
cat gsub_white_tab
hello this is a example
```
Use:
```
awk '{gsub(" |\t+",";");print }' gsub_white_tab
```
Result:
```
hello this;is a;example
```
#### File basename
get only filename and cat the path
```
echo "/opt/icinga/test/check_icinga" | awk '{gsub(/.*\//,"");print}'
check_icinga
```
Needed this when working on some CSV stuff
```
echo "/opt/icinga/test/check_test;somethingother;morestuff" | awk 'BEGIN{FS=";";OFS=FS}{base=$1;gsub(/.*\//,"",base);print base}'
```
Result:
```
check_test
```
#### Remove fields from row
```
row1;row2;row3;row4
```
Use:
```
echo "row1;row2;row3;row4" | awk -F ";" '{$2=$4=""; print}'
```
Result:
```
row1 row3
```
Can be done much easier if using cut
```
echo "row1;row2;row3;row4" | cut -d ";" -f-1,3
```
Result:
```
row1;row3
```
#### Remove whitespace from end of line
```
echo "whitespace at endline " |awk 'sub(/ *$/, "")'
```
## SED
#### Whitespace and Tabs
Search for two or more whitespaces and make them one space
```
cat whitespaces_tabs
Hallo World
This is Text with tabs and spaces
```
Use:
```
sed -e 's/[[:space:]]\{2,\}/\ /g' whitespaces_tabs
```
The syntax with `[[:space:]]` is for POSIX compatibility.
Result:
```
Hallo World
This is Text with tabs and spaces
```
#### more