awk

how to de-duplicate block (timestamp+command) from bash history?

how to de-duplicate block (timestamp+command) from bash history? I’m working with bash_history file containing blocks with the following format: #unixtimestampncommandn here’s sample of the bash_history file: #1713308636 cat > ./initramfs/init << "EOF" #!/bin/sh /bin/sh EOF #1713308642 file initramfs/init #1713308686 cpio -v -t -F init.cpio #1713308689 cpio -v -t -F init.cpio #1713308690 ls #1713308691 ls My …

Total answers: 2

awk inside function causing errors

awk inside function causing errors I’m trying to add the following to my .bashrc: cpu_info() { top -b -n1 | grep Cpu(s) | awk ‘{print $2 + $4}’ } but bash is telling me: bash: .bashrc: line 131: syntax error near unexpected token `(‘ bash: .bashrc: line 131: ` top -b -n1 | grep Cpu(s) …

Total answers: 1

Use a here-document to create a script for AWK

Use a here-document to create a script for AWK Why doesn’t this work? #!/bin/bash DOMAIN="example.com" PING_COUNT=3 #prints column header awk ‘BEGIN {printf("%-8s %-8s %-8s %-8s %sn", "avg", "min", "max", "mdev", "hostname")}’ FORMAT_RESULTS=$(cat << EOF ‘{ split($0, line, " = ") split(line[1], temp) hostname = temp[2] split(line[2], temp) split(temp[1], stats, "/") min = stats[1] avg = …

Total answers: 2

AWK: regex is not processed when passed as a variable

AWK: regex is not processed when passed as a variable Any suggestions? I read: Escape Sequences (The GNU Awk User’s Guide) Representative Example $ Filesystem=’/dev/mapper/vgubuntu-Media’ $ FilesystemRegex="$(echo "${Filesystem}" | sed "s~/~\/~g")" $ echo "$FilesystemRegex" /dev/mapper/vgubuntu-Media $ df -H "${Filesystem}" | awk -v Regex="${FilesystemRegex}" ‘/$Regex/{print $5}’ awk: warning: escape sequence `/’ treated as plain `/’ $ …

Total answers: 2

How can I replace the last occurrence of " before ] (text-manipulation)

How can I replace the last occurrence of " before ] (text-manipulation) I’m doing some text manipulation and I want to replace the last occurrence of " (with ", "four") before ] which could be in different lines. (this essentially adds a new item in the array) e.g: array = ["one", "two", "three"] other_array = …

Total answers: 5

How to remove double quotes within the double-quoted field values in .dat file

How to remove double quotes within the double-quoted field values in .dat file I have a text file that has around 15 columns.  The fields are separated by comma.  One column that is description is double-quoted and also has some words which are double-quoted.  I need to retain the beginning and ending double quotes and remove only the inner …

Total answers: 5

How can I add a property in the end of the object (text manipulation)

How can I add a property in the end of the object (text manipulation) I’m doing some text manipulation and I want to add a property (at the end) to an object named "object" that is inside a configuration file e.g . . object = { "one": "one", "two": "two", } . . Should become …

Total answers: 3

How would I do string comparison in awk with variable strings on both sides?

How would I do string comparison in awk with variable strings on both sides? As a follow-up to my previous question, how would a similar string comparison be made when there are mutable characters on either side of it in the document? As an example: # test,date "test1","27 March 2024 01:37 PM UTC" "test2","13 April …

Total answers: 1

Awk: Comparing passed Bash variables with column values

Awk: Comparing passed Bash variables with column values Using this example: #submission,date "test1","22 April 2024" "test2","24 April 2024" "test3","25 March 2024" "test6","01 April 2023" "test7","02 April 2022" "test8","03 April 2021" I’d like to only print tests in the present month, which as of writing is April 2024. Trying this command: awk -F, -v date="$(date +’%B …

Total answers: 1

awk – combine 2 steps into one

awk – combine 2 steps into one I need to read the output table of transmission-remote to determine if a download is taking place. This is currently achieved with trans="transmission-remote 0.0.0.0:9092 -n $user:$pass -l" [ "$($trans | awk ‘! /awk/ && /n[/]a/ {print $2}’)" == "n/a" ] || [[ $($trans | awk ‘! /awk/ && …

Total answers: 2

How to remove embedded newlines from CSV fields

How to remove embedded newlines from CSV fields I am struggling with processing a file. Normally I should have a line for each host definition. But sometimes, someone split some fields on different lines. Here is an example: "host1","host1","linux server","" "host2","host2","linux server","" Now I would like to find a way (better in bash) to fix …

Total answers: 7

syntax to delete lines

syntax to delete lines I use this syntax to delete lines from 2.txt to 1.txt: awk ‘NR==FNR{a[$0]=1;next}!a[$0]’ 2.txt 1.txt > lines.txt My lines are in this format: email@email.com:something If this line is identical in the two files, I get lines.txt without this, so it’s good, BUT I want to delete lines if the email@email.com is …

Total answers: 2

manipulate ls text output to add path to filenames

manipulate ls text output to add path to filenames I get sometimes files with following ls output format: /etc/cron.d: -rw-r–r– 1 root root 128 May 15 2020 0hourly -rw——- 1 root root 235 Dec 17 2020 sysstat /etc/cron.daily: -rw——- 1 root root 235 Dec 17 2020 sysstat Is there any chance using normal gnu tools …

Total answers: 6

sed to replace in a file, with both old and new strings in files

sed to replace in a file, with both old and new strings in files I want to automatically comment out a code block in PHP file, as below: The original block: // Enable all errors ini_set(‘display_startup_errors’, 1); ini_set(‘display_errors’, 1); error_reporting(E_ALL); The new block with comments: /* For the production version, the following codelines are commented …

Total answers: 3

How to apply AWK to a directory path?

How to apply AWK to a directory path? I have this command: Folder=./GAS/NNN_/Neutral awk -F / ‘{print $(NF-1)}’ $Folder But I got: awk: read error (Is a directory) I have tried: awk -F / ‘{print $(NF-1)}’ $(Folder) awk -F / ‘{print $(NF-1)}’ $($Folder) awk -F / ‘{print $(NF-1)}’ $(echo $Folder) And I got the same …

Total answers: 2

How to add content to a file before the last } character using Busybox utilities?

How to add content to a file before the last } character using Busybox utilities? There is a file with contents { "first_name": "John", "last_name": "Smith", "is_alive": true, "age": 27, "address": { "street_address": "21 2nd Street", "city": "New York", "state": "NY", "postal_code": "10021-3100" }, "phone_numbers": [ { "type": "home", "number": "212 555-1234" }, { "type": …

Total answers: 5