5 votes

Deleting all text after a specific string for multiple text files in a directory

Using jq (available for most Unix-like systems), you could extract the first JSON object from a file and discard the rest using jq -n 'input' file >newfile This produces "pretty-printed" ...
user avatar
  • 271k
5 votes

Write a regular expression whose output will only be rows in a range 01/03/2021 - 01/03/2020

Using the example file given, where dates are in order and the start + end date are present in the file, you might find a solution using awk to be more straightforward. $ awk '$1=="01/03/2020&...
user avatar
  • 20.1k
4 votes

awk/sed find indexes of the first and the last capital letter in a string

$ awk ' match($0,/[[:upper:]](.*[[:upper:]])?/) { print $0, RSTART, length()-(RSTART+RLENGTH-2) } ' file xyzAb 4 2 --AbbbAnde--- 3 7 abksjiRNNBBKUGFLYFYLF 7 1 -ankNUGUYUBUIGCafrg-- 5 7 ...
user avatar
  • 22.5k
4 votes
Accepted

awk/sed find indexes of the first and the last capital letter in a string

awk ' { start = match($0, /[A-Z]/) end = match($0, /[A-Z][^A-Z]*$/) print (start ? start : "NaN"), (end ? length() - end + 1 : "NaN") }' infile
user avatar
  • 38.4k
3 votes

awk/sed find indexes of the first and the last capital letter in a string

It is easy to get the length of the leading or trailing part with AWK. Add 1 to get the index as shown in the question. echo '--AbbbAnde--- abksjiRNNBBKUGFLYFYLF -ankNUGUYUBUIGCafrg-- BNKJUGFVULNK-Kew-...
user avatar
  • 5,433
2 votes

Write a regular expression whose output will only be rows in a range 01/03/2021 - 01/03/2020

From your description you need any date from year 2020 after 01/03/2020. That would be: $ egrep "(../(0[3-9]|1[0-2])/2020$)" dates.txt And also all dates from 2021 up to 01/03/2021. That ...
user avatar
2 votes
Accepted

Find missing values of column 1, then add those values and assign column 2 as 0

I don't usually offer code answers when there's been no obvious attempt. However, today it's sunny and here's my suggestion, awk ' { this = int($1*100 + .5) # Counter is ...
user avatar
  • 90.2k
2 votes

Write a regular expression whose output will only be rows in a range 01/03/2021 - 01/03/2020

Using Raku (formerly known as Perl_6) raku -ne 'my $ts = .subst(/ ^ (\d**2) \/ (\d**2) \/ (\d**4) /, {"$2-$1-$0"}).Date; say $ts if Date.new("2020-03-01") < $ts < Date.new (&...
user avatar
  • 1,220
2 votes

Replace values in fifth column

Okay, unfortunally, sed doesn't know that 10 is John. So if you don't want to write another substitution for each user, you might want to use a second file users.txt with a translation table like this:...
user avatar
  • 11.6k
2 votes

Replace values in fifth column

awk 'gsub(/0/, "", $5) { print $1, $2, $3, $4, "user" $5 }' INPUT gsub removes number "0" in the 5th column. Then awk prints column 1-4, and combines word "user&...
user avatar
2 votes

Replace values in fifth column

Two more awk solutions. $ awk '{$5="user"substr($5,1,1)}1' file Name 1:10:34 date short_id user1 Name 1:10:45 date short_id user1 Name 1:20:54 date short_id user2 Name 1:30:43 date short_id ...
user avatar
  • 20.1k
2 votes

Using AWK to sum up two column for each block

$ cat tst.awk BEGIN { FS=SUBSEP=";"; OFS=" = " } NF == 0 { prt(); delete sum } { print } NF > 3 { sum[$4,$5]++ } END { prt() } function prt( key) { for ( key in sum ) { ...
user avatar
  • 22.5k
2 votes

awk/sed find indexes of the first and the last capital letter in a string

Using Raku (formerly known as Perl_6) Sample Input: --AbbbAnde--- abksjiRNNBBKUGFLYFYLF -ankNUGUYUBUIGCafrg-- BNKJUGFVULNK-Kew--- nouppercaseletters oneUppercaseletter Skips lines with 0 or 1 ...
user avatar
  • 1,220
2 votes
Accepted

Write a regular expression whose output will only be rows in a range 01/03/2021 - 01/03/2020

I really wouldn't try to do this with just regular expressions. More sophisticated tools will make it easier. For example, with awk: $ awk -F/ '($3==2020 && $2 > 2) || ($3==2021 && (...
user avatar
  • 216k
2 votes

How to convert aggregated data to narrow 2 columns table by the first column of numbers

Awk solution An awk executable file conv_table.awk: #! /usr/bin/awk -f { # for all fields after the first column for (idx = 2; idx <= NF; idx++) { # print first column followed by ...
user avatar
1 vote

awk/sed find indexes of the first and the last capital letter in a string

GNU awk with field delimiter as an uppercase regex. LC_ALL=C \ awk -F '[A-Z]' ' NF>2{ print length("x"$1), length("x"$NF) }' file Perl has index & rindex builtins to get ...
user avatar
  • 5,398
1 vote

awk/sed find indexes of the first and the last capital letter in a string

{ s = match($0, /[A-Z]/); pad = RSTART clone = substr($0, RSTART + 1) while (match(clone, /[A-Z]/)) { clone = substr(clone, RSTART + 1) pad += RSTART } print $0, &...
user avatar
1 vote

Get value from a key value pair separated with pipe

With zsh instead of bash, storing the result in an associative array: $ string='Delete :xna.del|mode:Full|Execution: loaded' $ IFS=':|' $ set -o extendedglob $ typeset -A field=( ${=string//[[:space:]]...
user avatar
1 vote

Replace values in fifth column

Using Raku (formerly known as Perl_6) raku -ne 'given .words -> $w {put "$w.[0..3] ", (S/ (\d+) /user{$0.substr(0,1)}/ with $w.[4]) };' OR raku -ne 'put .[0..3], " ", (S/ (\d+) ...
user avatar
  • 1,220
1 vote

Ansible - How to reboot the server based on condition?

Please take note that a restart may be required not only after installing a new kernel but also after updates of microcode, glibc, SSL libs, etc.. So some more events will require a restart and needs ...
user avatar
  • 765
1 vote

Replace values in fifth column

Just use awk: $ awk '!($NF in users){ users[$NF]="user"(++cnt) } { $NF=users[$NF] } 1' file Name 1:10:34 date short_id user1 Name 1:10:45 date short_id user1 Name 1:20:54 date short_id user2 ...
user avatar
  • 22.5k
1 vote

Replace values in fifth column

Using sed sed 's/\(.*\) \(.\).*/\1 user\2/' input_file Name 1:10:34 date short_id user1 Name 1:10:45 date short_id user1 Name 1:20:54 date short_id user2 Name 1:30:43 date short_id user3 Name 1:40:43 ...
user avatar
  • 1,139
1 vote

Replace values in fifth column

If all you want to change is the last column you don't need the g modifier for global and you will want to add the $ anchor to limit it to the last column. I.e.: sed 's/\b10$/user1/' That is assuming ...
user avatar
  • 2,289
1 vote

Write a regular expression whose output will only be rows in a range 01/03/2021 - 01/03/2020

Just use awk: $ awk -F'/' '{d=$3$2$1} (20200301 <= d) && (d <= 20210301)' dates.txt 01/03/2020 13/03/2020 14/04/2020 29/05/2020 16/06/2020 17/07/2020 18/08/2020 19/09/2020 20/10/2020 21/...
user avatar
  • 22.5k
1 vote

Remove all characters from string, except specific multibyte range

Using Raku (formerly known as Perl_6) Sample Input (OP's text saved as a one-line file): ~$ cat peace.txt Peace be upon you. السلام عليكم. שלום עליכם. Paz sobre vosotros. Extract Arabic script: ~$ ...
user avatar
  • 1,220
1 vote
Accepted

Remove all characters from string, except specific multibyte range

You can use Unicode properties in Perl: echo "Peace be upon you. السلام عليكم. שלום עליכם. Paz sobre vosotros." \ | perl -CIO -pe 's/\P{Arabic}//g' -CIO tells Perl that input and output are ...
user avatar
  • 42.6k
1 vote

How to parse a particular ids from a text file?

Perl 5 solution $ perl -nle 'print join"\n",m/uniprot\":\"(.*?)\"/g' file.txt P12807 P12807 T12807 P12808 Z12809 P12821 P0C918 $
user avatar
  • 20.1k

Only top scored, non community-wiki answers of a minimum length are eligible