Skip to main content

New answers tagged

3 votes
Accepted

Format date output with spaces into awk

This has nothing to do with awk: $ date -d today +%d/%m/%Y %H:%M date: extra operand ‘%H:%M’ Try 'date --help' for more information. The date command takes one argument, but you are giving it two: +...
terdon's user avatar
  • 245k
-1 votes

How to convert multiple columns to rows in shell (unpivoting)

Using Miller, given $ mlr --tsv cat file.tsv OTUId AC0184 AC0250 AC0257 AC0258 AC0262 AC0263 AC0289 AC0293 OTU549024624 51659 0 0 0 0 0 0 0 ...
steeldriver's user avatar
  • 81.9k
0 votes

Delete 2-line paragraphs, keep 3-line paragraphs, maintain newline between all

Using any awk with a blank line at the end of the output: $ awk -v RS= -F'\n' -v ORS='\n\n' 'NF != 2' file aaa aaaaaa bb bbb cc cccc ccccc cccc fff ff ggg gggg gggg hhh hhh hh hhhh Using any awk ...
Ed Morton's user avatar
  • 32.4k
1 vote

Delete 2-line paragraphs, keep 3-line paragraphs, maintain newline between all

With GNU awk: awk 'BEGIN{FS=RS; RS=""} NF==3{print; print ""}' file Output: aaa aaaaaa bb bbb cc cccc ccccc cccc fff ff ggg gggg gggg hhh hhh hh hhhh Output contains one ...
Cyrus's user avatar
  • 12.4k
-1 votes
Accepted

Delete 2-line paragraphs, keep 3-line paragraphs, maintain newline between all

You can use perl for this (along with several other possibilities) perl -n -00 -e 'chomp; print "$_\n\n" unless y/\n// == 1' datafile Or expanded out somewhat, perl -n -00 -e ' # -n -00 ...
Chris Davies's user avatar
1 vote
Accepted

Using awk, append a value in last field in csv file based upon value in specific field

Here's one awk approach: ## This function takes as input an awk array whose indices are ## IDs and whose value is the number of times that ID has been seen. ## It returns the ID with the maximum value....
terdon's user avatar
  • 245k
1 vote

Using awk, append a value in last field in csv file based upon value in specific field

This sounds too complicated to do it reliably and quickly in awk. You'll want to treat your "row groups" individually. A program in a mainstream programming language with a reasonable CSV ...
Marcus Müller's user avatar
0 votes

how to round numbers after comma everytime up

It's been a long, long time since I used awk, and it's been even longer since I learned about integer math. You'll have to check if your numbers don't exceed limits, but... x = ((x * 103) + 99) / 100 ...
Fe2O3's user avatar
  • 101
3 votes

how to round numbers after comma everytime up

Using Raku (formerly known as Perl_6) ~$ raku -ne 'put ceiling($_ * 1.03)' file #OR ~$ raku -ne 'ceiling($_ * 1.03).put' file #OR ~$ raku -ne '($_ * 1.03).ceiling.put' file Raku can be used ...
jubilatious1's user avatar
  • 3,395
0 votes

script / command to get IPs from list of hostnames and combine into a single file

Here is a script if still required :) #!/bin/bash # Function to perform nslookup for an IP address perform_nslookup() { result=$(nslookup $1) echo -e "NSLookup for $1:\n$result" } #...
Tiborcz Kiss's user avatar
3 votes

how to round numbers after comma everytime up

I'm certainly biased towards using Perl, as it's often my tool of choice to do stuff, but short of using Bash itself or simpler tools like bc, which don't handle rounding up natively (of course it ...
kos's user avatar
  • 3,497
7 votes

how to round numbers after comma everytime up

To be clear, in echo 2.5 | awk '{print int($1+0.5)}' in your question, that awk code is not rounding up to 3, it's adding 0.5 and then rounding down to 3 (but it's already 3 so int() isn't actually ...
Ed Morton's user avatar
  • 32.4k
5 votes

how to round numbers after comma everytime up

Using Miller: $ mlr --nidx put '$1 = round($1 * 1.03)' file To round numbers to the upper integer, the following command may be used. $ mlr --nidx put '$1 = ceil($1 * 1.03)' file
Prabhjot Singh's user avatar
8 votes
Accepted

how to round numbers after comma everytime up

Let's assume your numbers are in a file numbers, and that your locale is one that uses , as a decimal point such as fr_FR.UTF-8. I have also added 10000000000 as one of your source numbers to ...
Chris Davies's user avatar
2 votes

how to round numbers after comma everytime up

you could use a script in which numbers are your input. Here's an example of a function that would help. for number in "${numbers[@]}"; do result=$(awk -v num="$number" 'BEGIN { ...
Rouuufa's user avatar
  • 64
0 votes

How to reduce the greediness of a regular expression in AWK?

Goawk is posix but supports non-greedy matches, so this just works without much work: echo "@article{gjn, Author = {Grzegorz J. Nalepa}, " | goawk '{ sub(/@.*?,/,""); print}' ...
saolof's user avatar
  • 101
0 votes

How can I comment out or in a section in source code with sed or awk

This popped up today. The solutions proposed seem overly complicated: /# BEGIN SOMENAME/ { prefix="# "; } /# END SOMENAME/ { prefix=""; } { print prefix $0; } (although the ...
symcbean's user avatar
  • 5,839
1 vote

Need to make multiple multiline replacements in file

Assuming you want to ONLY change blocks of text that match EXACTLY the block shown in your question and that your input always looks exactly as shown in your question then using any POSIX awk you ...
Ed Morton's user avatar
  • 32.4k
1 vote

Need to make multiple multiline replacements in file

After adding enclosing tag r to your XML: xmlstarlet ed -u '//filename' -v 'FILE_PATH/File Name' \ -u '//transname' -v '' file.xml <?xml version="1.0"?> <r> <type>...
Gilles Quénot's user avatar
1 vote

Cut and replace Nth character on every row

Using GNU awk: The following command is used taking cue from the answer: $ awk '{print gensub(/(^.{18})..(.*)/, "\\1x\\2", "g")}' file If input hasn't TAB as a character in input, ...
Prabhjot Singh's user avatar
0 votes

Getting output of /usr/bin/time in CSV format

Solution in TXR: We write a program that works like the external /usr/bin/time utility, taking arguments that are executed as a command, and which prints the CSV format: $ uname -s Darwin $ txr ...
Kaz's user avatar
  • 8,585
3 votes

Cut and replace Nth character on every row

As sed is neither in the title or tagged, and the input is predictable, I would offer: awk '{ print substr ($0, 1, 18) "x" substr ($0, 21); }' Has the advantage of being explicit, and the ...
Paul_Pedant's user avatar
  • 9,074
5 votes

Cut and replace Nth character on every row

If you need to replace the 19th and 20th character of each line with x if and only if they are , and space respectively, you can do: sed 's/^\(.\{18\}\), /\1x/' With most sed implementations (all ...
Stéphane Chazelas's user avatar
10 votes
Accepted

Cut and replace Nth character on every row

The simplest way given your example input is to just replace the first comma-and-space with an x (here, file has your example): $ sed 's/, /x/' file 775448167763476486x783834143007506433, 35972, ...
terdon's user avatar
  • 245k
1 vote

nmap output with awk to table

Another one nmap -sP 192.168.0.* | awk '/Nmap scan report for/{printf $5;}/MAC Address:/{gsub("\(|\)", "");print " "$3 " " substr($0,index($0,$4)) }'...
Dorian Grv's user avatar
0 votes

regular single column data to multiple rows

Using GNU awk for multi-char RS and RT: $ awk -v RS='KKK-SNMOdb: NONE\n' -F'\n' -v OFS=',' '{ORS=RT; $1=$1; print}' file KKK-SNMImsi: 444444,KKK-SNMUserProfileId: KKK-SNMDefaultAutomaticProfile,KKK-...
Ed Morton's user avatar
  • 32.4k

Top 50 recent answers are included