Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now

Hot answers tagged

34

This is what the cut command is for. cut -d';' -f2-


19

With date, shuf and xargs: Convert start and end date to "seconds since 1970-01-01 00:00:00 UTC" and use shuf to print six random values in this range. Pipe this result to xargs and convert the values to the desired date format. Edit: If you want dates of the year 2017 to be included in the output, you have add one year -1s (2017-12-31 23:59:59) to the end ...


14

Another sed variant: sed 's/^[^;]*;//' file Replace any non-semicolon characters ([^;]*) at the beginning of the line (the first ^) followed by a semicolon with an empty string.


10

You can turn the problem into generating a random number between a number representing the first possible date and a number representing the last possible date (actually the one right after the last possible), in unix epoch format. Everything else is handled by standard date conversions. gawk has a better random number resolution than bash (float vs 15 bits ...


10

Perl: perl -MTime::Piece -sE ' my $t1 = Time::Piece->strptime("$y1-01-01 00:00:00", "%Y-%m-%d %H:%M:%S"); my $t2 = Time::Piece->strptime("$y2-12-31 23:59:59", "%Y-%m-%d %H:%M:%S"); do { my $time = $t1 + int(rand() * ($t2 - $t1)); say $time->dmy; } for 1..6; ' -- -y1=1987 -y2=2017 Sample output: 10-01-1989 30-04-...


9

Inside the command substitution you have "$FILE" | sed -e 's/\\/\//gp', which the shell expands to (the equivalent of) '\\edi3\welshch\test' | sed -e 's/\\/\//gp'. Since it's a command, the shell goes looking for a file called \\edi3\welshch\test to run. You probably meant to use echo "$FILE" | sed ... to pass the contents of FILE to sed via the pipe. ...


9

Here's one way, mostly in awk: #!/bin/sh [ "$end" -ge "$start" ] || exit 1 awk -v start=$1 -v end=$2 ' BEGIN { srand(); for(i=1; i <= 6; i++) printf "%02d/%02d/%d\n", 1 + rand() * 28, 1 + rand() * 12, start + rand() * (end-start); } ' < /dev/null The shell script takes two parameters and passes them as variables to awk, who reads ...


8

Using variable name prefix matching and variable indirection in bash: proj_env_repo_db_username=username proj_env_repo_db_host=host proj_env_repo_db_port=port for variable in "${!proj_env_repo@}"; do export "TF_ENV${variable#proj_env_repo}"="${!variable}" done The loop uses "${!proj_env_repo@}" to generate a list of variable names that share the name ...


8

In awk: awk 'BEGIN { FS = ";" } ; { print $2 }' or, even simpler awk -F";" '{print $2}' (thank you, ilkkachu, for this simplified version)


6

With GNU sed: sed -E 's/^.*;(.*)/\1/' file With GNU grep: grep -Po '(?!.*;).*' file


6

Run this from your terminal to get a random 11-digit number generated each time: $ tr -cd '[:digit:]' < /dev/urandom | fold -w 11 | head -n 1


6

< inputfile egrep ';' | cut -f 2 -d ';' -This command and you will get this!


6

You simply have an extra / at the beginning, which confused sed into thinking that you want to perform a command (called <) on a line that matches the pattern /s/. Instead, use: sed -i 's/<ADD ANSWER HERE>/answer/' config.yaml


6

To replace single quotes (') it's easiest to put the sed command within double quotes and escape the double quote in the replacement: $ cat quotes.txt I'm Alice $ sed -e "s/'/\"/g" quotes.txt I"m Alice Note that the single quote is not special within double quotes, so it must not be escaped. If, instead one wants to replace backticks (`), as the ...


5

There are, as the existing answers already show, many ways to do this. Here's another GNU grep approach: $ grep -oP ';\K.*' file 1563101282.M178569P409830.de122.abteilung.com,S=1258,W=1287:2, 1563102004.M49870P436474.de122.abteilung.com,S=1258,W=1287:2, 1563102961.M195946P457876.de122.abteilung.com,S=1258,W=1287:2, 1563103921.M334168P463856.de122.abteilung....


5

Use jq to parse json data: jq -r '._source.firstname' With the input data from the question it shows the desired output.


4

If the difference in the number of years is less than about 90 you can use the $RANDOM variable in bash to give you an offset in number of days and use the limited ability of the date command to do the calculation. #!/bin/bash s=$(date +%s -d "1/1/$1") # start in seconds since 1 Jan 1970 e=$(date +%s -d "1/1/$(($2+1))") # start of end year +1 in ...


4

Considering that this input is Data01.txt, it may be done as: awk -F";" '{ print $2 }' Data01.txt > Data01\ Ready.txt Which will output Data01 Ready.txt file.


4

I'd use awk for that: $ cat tst.awk (!NF) { # blank line b = ""; f = 1 # empty buffer, start buffering } /-->/ { # timestamp f = 0 # stop buffering if (p == $0) { # same timestamp next # discard buffer, start over } p = $0 # save timestamp printf "%s"...


3

Try this: od -An -N8 -d /dev/random | sed -e 's| ||g' -e 's|\(.\{11\}\).*|\1|'


3

while IFS='=' read -r name value; do new_name="TF_ENV${name#proj_env_repo}" export "$new_name=$value" unset "$name" done < <( printenv | grep proj_env_repo ) The secret ingredients here: Shell Parameter Expansion IFS='=' read -r name value -- uses the = sign to split the incoming line into the component parts. using a Process Substitution ...


2

Assuming that you have that list in list.txt, you can do the following: grep -h L list.txt > list_L.txt Explanation: grep L list.txt gives you all the lines containing an uppercase L. The option -h suppresses the output of the file name, but is only necessary if you are searching through multiple files (e.g. grep -h L lis*.txt). Adding > list_L.txt ...


2

Another grep example: grep -E "[0-9]+ +L" file + matches one or more occurrence of the previous character, so in this case we match at least one numeric character, and at least one space, followed by a capital L. -E is required to interpret the + as a special character instead of a literal '+' output: 59 LOUIS 1202 +1 62 LEON 1137 +12 73 ...


2

With shuf from GNU coreutils: shuf -n1 -i0-99999999999 Increase the value of the -n option if you need more than one number.


2

In "1,$1p", the $1 is expanded by the shell, and sed sees 1,actorsp. actorsp isn't a valid sed address, so it gives an error. The error text looks a bit weird to me though, it seems to recognize a as a command and then go back to wondering about the comma. In '1,$1p', the $1 is not expanded by the shell, and sed sees 1,$1p. $ is a valid address, it's used ...


2

/etc/hosts entries can have one or more hostnames per IP address, so we need to check each hostname (i.e. all fields except the first field) to see if it has 2 or more . characters in it. For example: awk '{for (f=2;f<=NF;f++) {if (split($f,array,/\./)>2) {print;last}}}' /etc/hosts or, with linefeeds and indentation added to improve readability: ...


2

To substitute individual characters, it's simpler to use tr(1): FILE=$(echo "$FILE" | tr '\\' / )


2

A more general answer, since I can't comment on geekosaur's answer - You wouldn't put the ^ (start of line anchor). For example, if you want to replace the first occurence of A with B, you would have sed 's/A/B/'


2

The variable has to be output (by echo) to sed. Using sed -n to suppress sed output. Edit - don't need sed -n if we omit the sed p flag. #!/bin/bash FILE='\\edi3\welshch\test' FILEPATH="$(echo "$FILE" | sed 's/\\/\//g')" echo $FILEPATH


2

sed -n '/Red Hat Satellite/,/Pool ID:/{ s/.*Pool ID:[[:blank:]]*\(.*\)/\1/p; }' file This applies a substitution that picks out your string from after Pool ID:. It does this on any line between a line that contains the string Red Hat Satellite and the first line afterwards that contain Pool ID:. The command would print only the result of any successful ...


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