Updated awkward.

This commit is contained in:
CSnap 2023-11-19 15:26:51 +00:00
parent 67bd029e27
commit 85037f19c3
1 changed files with 18 additions and 18 deletions

View File

@ -7,7 +7,7 @@ These examples explore the combined use of AWK and SED for text processing tasks
### Input File (input.txt) ### Input File (input.txt)
``` ```
John Doe,25,Engineer Carl Sanchez,25,Engineer
Jane Smith,30,Designer Jane Smith,30,Designer
Bob Johnson,22,Developer Bob Johnson,22,Developer
``` ```
@ -29,7 +29,7 @@ Name Age Job\
``` ```
Name Age Job Name Age Job
--------------- --- ---------- --------------- --- ----------
John Doe 25 Engineer Carl Sanchez 25 Engineer
Jane Smith 30 Designer Jane Smith 30 Designer
Bob Johnson 22 Developer Bob Johnson 22 Developer
``` ```
@ -47,20 +47,18 @@ date
### Goal ### Goal
Print lines containing "a" or "e" using AWK and SED. Print lines containing "c" or "r" using AWK and SED.
### Solution ### Solution
```bash ```bash
awk '/a|e/' input.txt | sed -n '/a\|e/p' awk '/c|r/' input.txt | sed -n '/c\|r/p'
``` ```
### Output ### Output
``` ```
apple cherry
banana
date
``` ```
## Example 3: AWK and SED for Filtering and Sorting ## Example 3: AWK and SED for Filtering and Sorting
@ -116,7 +114,7 @@ awk 'BEGIN{RS="\\."} /sed/' input.txt | sed 's/sed/AWK/g'
### Output ### Output
``` ```
Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua AWK do eiusmod tempor incididunt ut labore et dolore magna aliqua
``` ```
## Example 5: AWK and SED for HTML Tag Extraction ## Example 5: AWK and SED for HTML Tag Extraction
@ -145,8 +143,10 @@ sed -n 's/<[^>]*>//p' input.html | awk '{print tolower($0)}'
``` ```
div class="container" div class="container"
p p
/p
a href="https://example.com" a href="https://example.com"
/a /a
/div
``` ```
## Example 6: AWK and SED for JSON Formatting ## Example 6: AWK and SED for JSON Formatting
@ -155,9 +155,9 @@ a href="https://example.com"
```json ```json
{ {
"name": "John Doe", "name": "Carl Sanchez",
"age": 30, "age": 30,
"city": "New York" "city": "Indianapolis"
} }
``` ```
@ -175,9 +175,9 @@ awk -v RS= '{$1=$1}1' input.json | sed 's/":"/": "/g; s/{/{\n/g; s/}/\n}/g'
``` ```
{ {
"name": "John Doe", "name": "Carl Sanchez",
"age": 30, "age": 30,
"city": "New York" "city": "Indianapolis"
} }
``` ```
@ -335,9 +335,9 @@ sed -n '/<li>/s/<li>\(.*\)<\/li>/\1/p' list.html | awk '{print "- " $0}'
### Input File (data.yaml) ### Input File (data.yaml)
```yaml ```yaml
name: John Doe name: Carl Sanchez
age: 30 age: 30
city: New York city: Indianapolis
``` ```
### Goal ### Goal
@ -354,9 +354,9 @@ sed -n '/^[^#]/s/: /: "/p' data.yaml | awk '{printf "%s", $0} END {print "\""}'
``` ```
{ {
name: "John Doe", name: "Carl Sanchez",
age: "30", age: "30",
city: "New York" city: "Indianapolis"
} }
``` ```
@ -481,7 +481,7 @@ orange 3
### Input File (json_data.json) ### Input File (json_data.json)
```json ```json
{"name":"John","age":30,"city":"New York"} {"name":"John","age":30,"city":"Indianapolis"}
``` ```
### Goal ### Goal
@ -500,7 +500,7 @@ awk -v RS= '{$1=$1}1' json_data.json | sed 's/":"/": "/g; s/{/{\n/g; s/}/\n}/g'
{ {
"name": "John", "name": "John",
"age": 30, "age": 30,
"city": "New York" "city": "Indianapolis"
} }
``` ```