Tag Info

New answers tagged

0

Since you asked for the year, ls -lac is an easy one to remember if, like me, you use ls -la all the time. The c gives you ctime which will display a year if it's not the current year or the hour and minute if it is.


3

For ksh93, you have (at least) a couple of choices associative arrays envir=Dev foo["$envir"]=bar echo "${foo["$envir"]}" namerefs nameref var=${envir}foo var=bar echo "$var" For ksh88, you may be stuck with eval: envir=Dev name="${envir}foo" eval "$name=bar" eval "echo \$$name"


1

How about: tr '|' '\n' | sed -n 's/=Y$//p'


1

perl -nE 'say join(",",/(\w+)=Y/g)'


0

Try to run the following command apt-get update before executing apt-get install command Also make sure that urls in /etc/apt/sources.list are available.


2

awk can read records based on a regex delimiter of your choice. eg '[|\n]' It can also split records into fields on the delimeter of your choice. eg. '=' The ternary operator (condition)?: prevents a leading comma. awk -F= -vRS='[|\n]' '$2=="Y"{ printf (i?",":"")"%s", $1; i=1 }' output: p2,p6 If a trailing newline is needed, it can be appended in ...


5

Using Perl compatible regular expression in grep: grep -Po '..(?==Y)' <file Result: p2 p6


1

a simple awk awk -F\| '{for (i=1 ; i<= NF; i++) if ( $i ~/Y/ ) { split($i,A,"=") ; printf "in %d : %s\n",i,A[1] ;}}' where -F\| use | as separator {for (i=1 ; i<= NF; i++) scan through pattern if ( $i ~/Y/ ) if found { split($i,A,"=") ; printf "in %d : %s\n",i,A[1] ;} split it and print it output in 3 : p2 in 11 : p6 use printf ...


3

Try this: echo 'p1=X||p2=Y||p3=X||p4=X||p5=X||p6=Y||p7=X' | grep -o '[^|]*=Y' | cut -d= -f1 | sed -e 'N;s/\n/,/g' Output: p2,p6


0

It's only the first sed which needs modifying to handle all of the possibilities you list here. Specifically, rather than handling all possibilities in a single regexp, instead you'll need to address two different kinds of lines separately - the kind on which the ID immediately follows the first - dash on a line, and the other kind on which it is found ...


1

If you interested just in SUM grep -cf <(sed s/^/\^ACTIVE,/ city_name) siteDBName or for big files grep -f city_name siteDBName | grep -c ^ACTIVE


2

You have two basic choices: i) use -v to pass the variable to awk or ii) close the ' around the awk script, use the shell variable and continue the ' again. Use -v while read city do awk -vcity="$city" -F, '{ if ( $1 == "ACTIVE" && $2 == city ){print $1} }' siteDBName >> count ...


1

Just as @cuonglm and @YoMismo stated, you are using the wrong variable and the wrong way to reference it. It should be something like: while read city do awk -v c="$city" -F, '{ if ( $1 == "ACTIVE" && $2 == c ) print $1 }' siteDBName >> count SUM=`awk '{ sum += $1 ...


6

The test [[ -t 1 ]] returns true if File descriptor 1 (STDOUT) is opened on the terminal, otherwise false. From help test in bash : -t FD True if FD is opened on a terminal.


3

[ -t 1 ] ...or... test -t 1 ...return true if file descriptor 1 - stdout - is a tty and false otherwise. The same is true of [[ -t 1 ]] ...in many shells.


0

Parallel FTW (no doubt you have more than one core - why not use them?): ls *flac | while read f; do ffmpeg -i "$f" -acodec libmp3lame $f.mp3 & done


0

#!/bin/bash while read JB last do autorep -j $JB -q done < RMANJOBS | sed -rn "/^\/|^insert_job|^description/{ s/(description:.* [Ii]f failed )[^"]*/\1Send to Karn Kumarl/ s/insert/update/ p }" | sed '0~3 G' > MRMANJOBS


1

There are, of course, many bash scripts on the 'net that folks use for checking NTP/NFS issues. Without seeing at least a rudimentary rough draft of the script, it would be hard to say just how useful something like this might be. Are you thinking of readline's with if/then & case statements to implement such a script? I can't speak for other ...


3

In your link, the variable used outside (linecount) is not defined as a "loop" variable. It's just modified (incremented) inside the body loop, but not in the "while" statement. This is because when the "read -r f1 f2..." part is called, it reset (prepare) the variables used (f1 ..f7), wait for an input line and assign the variables according to the input. ...


1

df -h | grep '[6-9][0-9]%\|100%' will grep anything with 60% or more usage


4

No need for a script. It's a one-liner with a minimum of awk : df -hlP | awk 'int($5)>60' If you want to change the output, just use awk 'print' : df -hlP | awk 'int($5)>60{print "Partition "$1" has only "$4" free}' And pipe it to your mailer : df -hlP | awk 'int($5)>60{print "Partition "$1" has only "$4" free}' | mail -s "Disk usage on ...


1

Your best bet is to institute a monitoring solution, such as Xymon (http://downloads.sourceforge.net/project/xymon/Xymon/4.3.21/xymon-4.3.21.tar.gz?r=&ts=1438001054&use_mirror=iweb) that can trend your disk usage over time. You simply select the icon under the Disk colunmn and it will show you what it currently is and what it was when Xymon first ...


1

Based on @mikeserv approach I'm getting following output. SCRIPT: ( sed -e'y/)},={/(((((/' \ -e's/-\([^(I]*\)[^0-9]*\([0-9]*\)[( ]*/- \2 -\1/;=' | paste -d- - - | sort -t- -nk3,3 -nk1,1 | sed -e's/^[^-]*-//;:n' -e'h;$!N' \ -e's/\(-\([^-]*-\).*[^ ]\) *\n\([^-]*-\)\{2\}\2/\1 - \3/;tn' \ -ex\;:t ...


0

[[ -n $(df -h / | awk 'int($5)>6{print $5}') ]] && echo "Warning file system greater than 6%"


2

You want (without any spaces around the =) usage2=$(awk '{print $5}' /home/peters/usage1 | tail -n1 |cut -c1) to put into the usage2 variable the output of your awk...|cut -c1 command. You need a space between the awk braced command and its input file name (but no spaces around =). BTW, you could simply use cut(1) as cut -f5 (instead of awk '{print ...


2

Your line 17 should be: if [ $usage2 -gt 6 ] You need to put a space after [ and before ]. Also, when comparing numbers, you should be using -eq, -ne, -gt, -lt, -ge, and -le rather than the familiar symbols.


1

Try writing your if statement like this: if [ condition ]; then Stuff here fi


4

Set your Warning options as an array. "${warnings[@]}" generates 3 individual words warnings=(-Wall -Wextra -Wpedantic) "${compiler}" "${warnings[@]}" "${standard}" -o "${1}" "${1}.cpp" Or, if you find it more legible, you can create the array without -W's, and then add -W's via how you present the array on the command line. warnings=( all extra ...


13

The function returns, but the command substitution blocks, because you created a background job, but you still have your stdout fd opened. Just close it by adding >/dev/null before the &. #!/bin/bash function start { leafpad >/dev/null & echo $! } PID=$(start) echo "PID is $PID" If you want your process to have also stdin, stdout, stderr ...


0

You are not giving any argument (filename) to stat leading to that error. You are using variable file to store the each filename and loop over them but you are not giving the filenames as arguments to stat. In a nutshell you need to pass $file (the value of variable file) to stat. Also you can do all you are doing in one line : for file in .* *; do stat ...


0

If you can get by on just the timestamps, the following is enough: sed -e:n -e'$!N;s/^\(\([^-]*-\).*\)\n *\2/\1:::/;tn' -eP\;D <in >out It recursively appends the Next line to the current one, and, if all of the characters at the head of the current line up to and including the first - dash can match the head of the appended line, the two are joined ...


0

So as IDs in each line in 6th field or in beforelast field collect all lines by ID is possible w/out sub awk -F"[ }=)]+" ' NF{ if($6 ~ "[0-9]{3}") ids=$6 else ids=$(NF-1) if(!M[ids]) M[ids]=$0 else M[ids]=M[ids] " " $0 } END{ for(i in M) print M[i] }' /var/tmp/text.txt


2

You could just do the whole thing in awk. The following combines the IDs as they were read. awk '{ line = $0; # ID is { XXX } or ( XXX ) if ( /[{(] *[0-9]+[})]/ ) { id = $0; sub(/ *[})].*/,"", id); sub(/.*[({] */,"", id); } # ID is ID=XXX else if ( $NF ~ /ID=/ ) { id = $NF; ...


1

You have the commands, so put them in a script! To run a bunch of commands on different data, put the changing data in a variable. To run gcov and mv on all the files, there are several possible methods, including: Run gcov on all files, then move them. Run gcov on one file, then move its output. Run gconv on the files in a directory, then move them. ...


3

This is a really weird requirement. Why would you care what interactive shell invoked your script, or invoked some other program that isn't an interactive shell which in turn invoked your script? This has a very strong smell of a XY problem. IF you really need to know, you can try to figure it out, but I don't think there's a fully reliable method, just one ...


0

If you want to be more bash specific you can also write you code in ~/.bash_profile or ~/.bash_login. And you can source any script therein for example: if [ -f ~/.bashrc ]; then source ~/.bashrc fi


1

You could try like this: for file in *Block* do echo mv "$file" "${file//[ ()@$]/_}" done If you're happy with the result, remove the echo before mv to actually rename the files.


0

Since the rename command didn't work for me for unknown reasons and i do not get any other answers for my question, i myself tried to make an effort to make the rename possible. This might not be the best approach to rename the files but it worked for me and this is why i would like to post it as an answer so that if anyone else reads this might get some ...


2

You check its options. [ "$-" = "${-#*i}" ] || echo shell is interactive You can also check its file descriptors. This is a little different. It won't necessarily tell you if the shell is interactive per se, but it will tell you if it's talking to a terminal. for fd in 0 1 2 do [ -t "$fd" ] && break done|| echo shell fds 0 1 2 are ...


0

So I copied your bottom thing into two files. File 1: { "section1Configs": { "level1Settings": { "section01Lev01_01": true, "section01Lev01_012": true, "section01Lev01_02": true } } } And File 2... { "section1Configs": { "level1Settings": { "section01Lev01_01": true, ...


0

May be better to use tools specialized for json like jq or jshon? But if you'd like it can be done with paste and sed paste config master | sed '/[{}]/! {/\(.\+\)\t\1/d;};s/\t.*//' or awk awk '{getline a < "master"} /[{}]/ || $0 != a' config for each line in config-file gets corresponding(by number) row from master-file into variable a and if line ...


0

You can do it with awk (only tested with GNU awk) like awk '{print $0;match($2, /=(.*),/, arr); if(arr[1]!="") {print "changetype: modify\nadd:mail\nmail: " arr[1] }}' <input file> This will print each line print $0, then use match to pull out the email address. Then it will print the stanza you want, assuming it found an email address to print. ...


1

This might work for you: sed -e 's/.*cn=\([^,]*\).*/&\nchangetype: modify \nadd: mail \nmail: \1/'


3

an awk answer: this will keep the order the questions the same as in the source file. $ awk '{filename = "questions" ++n[$2] ".txt"; print > filename}' questions.txt $ cat questions1.txt Q.1 2 Marks Q.2 5 Marks Q.3 4 Marks Q.4 3 Marks Q.5 6 Marks $ cat questions2.txt Q.6 4 Marks Q.7 3 Marks Q.8 2 Marks Q.9 6 Marks Q.10 5 Marks


0

Create a .desktop file in your ~/.local/share/applications folder for the script (or in usr/share/applications for system wide access). E.g.: [Desktop Entry] Name=My bash script Comment=bash script to do custom stuff Type=Application Exec=/path/to/bash/script %U Icon=/path/to/some/fancy/icon Terminal=false (or true) Categories=Other; It will show up in ...


7

That's because you are checking whether the string gpio -g read 22 is greater than 1. Since gpio -g read 22 is not a number, you get that error. You don't explain what you are trying to do but I'm guessing you want to compare the output of the gpio command. To do that, you need to enclose the command in $() or backticks (``): x=$(gpio -g read 22) if [ ...


2

Try something like this: #!/bin/bash if pgrep "mysql" > /dev/null then echo "MYSQL Running" rm -f /var/run/.mysql_mail_sent else echo "ALERT Stopped" if [ ! -f /var/run/.mysql_mail_sent ]; then // send your mail here date > /var/run/.mysql_mail_sent fi fi


1

From man find EXIT STATUS find exits with status 0 if all files are processed successfully, greater than 0 if errors occur. This is deliberately a very broad description, but if the return value is non-zero, you should not rely on the correctness of the results of find Thus if there is no matched files the exit status is remain 0. So better to check ...


6

sort -n -k2 -k1.3 file | awk '{$2!=a?x=1:x++} {print > "file"x; a=$2}' First , we need to sort the file correctly. -n sorts the file numerically, -k2 sorts according to the second field (the marks 2-6), -k1.3 then sorts within this order the first field starting from the 3rd character numerically (irgnoring the leading Q.). Now awk splits the output ...


0

find $(find -name "[0-9][0-9][0-9][0-9]") -name "[test]*" -mtime -7 -exec rm -i {} \; || echo "There are no recovery files older than 7 days" The || (or) operator looks at the result of the command on its left, and runs the command on the right only if the command on the left failed (based on the return code). If find does not find any matching files, it ...



Top 50 recent answers are included