Tag Info

Hot answers tagged

17

The right tool for this job is probably paste paste -d '' file1 file2 See man paste for details.


9

Using python: #!/usr/bin/env python2 import re, sys with open(sys.argv[1]) as f: for line in f: parts = line.split() for i in parts: if re.search(r'^[("].*[)"]$', i): print i, print '\n'.lstrip() Output: "foo" (bar) (19) "foo" Every line is read and parts separated by spaces are saved into a ...


6

New version (spaces allowed between () or ""): Try the below perl command (credits: @steeldriver) perl -ne 'printf "%s\n", join(" " , $_ =~ /["(].*?[)"]/g)' Initial version (no spaces between () or "") You can try the following perl oneliner: $ perl -ne '@a=split(/\s+/, $_); for (@a) {print "$_ " if /[("].*?[)"]/ };print"\n"' file


6

You can use sed, perl and other tools for that. Here are two examples for sed and perl: Use sed: Start a dry run sudo sed 's/^GRUB_TIMEOUT=10$/GRUB_TIMEOUT=2/' /etc/default/grub if it's ok, then sudo sed -i 's/^GRUB_TIMEOUT=10$/GRUB_TIMEOUT=2/' /etc/default/grub from man sed: -i[SUFFIX], --in-place[=SUFFIX] edit files in place (makes ...


5

# in some configuration files and programming languages marks comments and is excluded from execution. Run a command and then create a hashtag using # of the command so that we can find it later. some-command #mycommand for example: sudo dmidecode | grep -A 9 "System Information" #sysInfo Now I create a hash tag #sysInfo for the command sudo ...


5

a=1 b=3 printf -v solution "%.2f" $(bc -l <<< "$a / $b") echo $solution Output: 0.33 If $a and $b are always integers and you are looking for bash only solution to divide two integers: Then you might be interested in this.


5

Another python option: #!/usr/bin/env python3 import sys match = lambda ch1, ch2, w: all([w.startswith(ch1), w.endswith(ch2)]) for l in open(sys.argv[1]).read().splitlines(): matches = [w for w in l.split() if any([match("(", ")", w), match('"', '"', w)])] print((" ").join(matches)) Copy the script into an empty file, save the script as ...


5

Something like this should do: for file in ./*_???ILN*; do dir=${file%ILN*} dir=${dir##*_} mkdir -p "./$dir" && mv -iv "$file" "./$dir" done See FAQ 100 for more on string manipulations in bash.


4

It is failing because you are searching in the current directory and not the target. Change your find to: find "$d" -type f -exec ls -al {} \; | sort -nr -k5 | head -n 1 However, a safer way would be: #!/bin/bash for d in */ ; do file=$(find "$d" -type f -printf "%s\t%f\n" | sort -n | tail -n 1 | cut -f 2- ) echo "$file" mv "$d${file}" . done ...


4

As perl script: $filename=$ARGV[0]; if (open(my $fh, '<:encoding(UTF-8)', $filename)) { while (my $match = <$fh>) { while ($match =~ /((\(.*?[^)]\))|(".*?"))/g) { print "$1 "; } print "\n" } } Or as perl one-liner: perl -ne 'while (/((\(.*?[^)]\))|(".*?"))/g) {print "$1 ";} print "\n"' file Output "foo foo" (bar bar) ...


4

If you (or someone else with a similar problem who reads this) don't need to preserve the newlines, the following would work: grep -Eo '"[^"]*"|\([^)]*\)' For input 19. "foo foo" (bar bar) (19) raboof "foo foo" raboof it yields output "foo foo" (bar bar) (19) "foo foo" If you need newlines, you can use some tricks, e.g. this: sed 's/$/\$/' \ | grep ...


4

bc will not print a leading zero. You can print the output by piping it to awk '{printf "%0.2f", $1}' To summarize: solution=$(echo 1 / 3 | bc -l | awk '{printf "%0.2f", $1}') echo $solution


4

You want https://launchpad.net/undistract-me (installable from the Ubuntu archives with sudo apt-get install undistract-me) which does precisely what you're asking for, including working automatically (that is, without having to remember to add something extra to potentially long-running commands).


3

EDIT TL;DR: create autocompletion shortcut in .inputrc and function in .bashrc . Run command as usual, type in, but instead of ENTER, press the shortcut that you specified in .inputrc The person who placed bounty on this question said: "All of the existing answers require typing an additional command after the command. I want an answer that does ...


3

While this is indeed possible in sed or awk, it is much easier (for me, anyway) using Perl: $ perl -ple '@pars=( /(\(.+?\))/g ); for $par (@pars){ s/\s*.$par.// unless $par=~/blat|bar/ } s/[()]//g;' file foo bar 80 foo blat 92 Explanation -ple : print each line of the input file, after executing the script on ...


3

Using python: #!/usr/bin/env python2 import re with open('/path/to/file.txt') as f: for line in f: pat_list = re.findall(r'\(([^)]*?)\)', line.rstrip()) for pat in pat_list: if not re.search(r'(?:blat|bar)', pat): print re.sub(r'\(|\)', '', line.replace(' ({0})'.format(pat), '').rstrip()) Output: foo bar 80 ...


3

Using awk: Save the following code into a text file and make it executable (chmod u+x filename). Then, run it like this: awk -f filename inputfile This is huge compared to solutions in perl or python, I'm adding this just because awk or sed was the preferred solution and to show that it's possible to use awk even though it's not convenient. { #list ...


3

Using sed: < inputfile sed 's/(\([^\)]*\(bar\|blat\)[^\)]*\))/\1/g; s/(.*) //g' Input file: test (bar) (blat) bar (testblat) (bartest) blat (testbar) (barblat) (no) (blatanother) Output file: test bar blat bar testblat bartest blat testbar barblat blatanother Breakdown: #1: (: matches a ( character \(: starts grouping the capturing group ...


3

With printf: $ printf '%f' $(echo $a / $b | bc -l) 0.200000 If you want only 1 position after decimal point: $ printf '%.1f' $(echo $a / $b | bc -l) 0.2


3

You can use if conditional construct to perform an action depending on some condition e.g. if something exists or not. In your case you need to put the action segment inside the if-then condition: if ! [[ -f "_thumb_wd_${f%.pdf}.jpg" ]]; then convert -thumbnail 250x200 "$f"[0]"_thumb_wd_${f%.pdf}.jpg" fi [[ is a bash keyword, we are using it to ...


3

I have found the answer. As it turns out, there was an application called sheepdog causing the problem. I first removed the application and then entered into the directory /etc/bash_completion.d and removed the directory as follows: sudo rm -rf sheepdog and restarted the terminal. It worked perfectly.


3

*/15 * * * * . /home/ashish/parser.sh cron uses sh, not bash, so when you source the script (that's what the . does), it is run under sh, not bash. Remove the .. Also, the PATH for cron is limited. Specify the full paths to commands you use, such as workon, or set PATH yourself.


3

Sounds like your script lacks a shebang line. Make sure the very first line of the script reads: #!/usr/bin/env bash or #!/bin/bash On a side note, you should avoid putting .sh extension on a bash script, since bash is not sh. Preferably use no extension at all.


3

In order to be sure no line break you can use other option than c. split -n #number split a file by #number chunks so you can thus be sure they are equal and more no break in lines.Example split -n 4 file.txt This will split the file.txt into 4 chunks. OR you can split by number of lines split -l #number-of-lines example split -l 200 file.txt ...


2

while read file; do split <some options> "$file"; done < list.txt If your file names can contain whitespace or backslashes, use this instead: while IFS= read -r file; do split <some options> "$file"; done < list.txt


2

Sounds like the script contains some CR (\r) characters. If you have edited this script from windows, that would explain why. Running this should reveal the otherwise "invisible" carriage return characters: sed -n l /home/pi/sh/test.sh (That's lowercase L, not 1). If you see a \r in the above, see FAQ 52 for various ways to get rid of them. On a side ...


2

Use this find "$d" -type f ... instead of find . -type f ... . is your current folder and you mean the elements provided by $d.


2

Another perl: $ perl -nle 'print join " ", $_ =~ /".*?"|\(.*?\)/g' file "foo foo" (bar bar) (19) "foo foo"


2

Below simple python code will do this job. import re with open('file') as f: reg = re.compile(r'"[^"]*"|\([^)]*\)') for line in f: print(' '.join(reg.findall(line))) And another one through Perl which uses only regex, $ perl -pe 's/(?:"[^"]*"|\([^)]*\))(*SKIP)(*F)|\S//g;s/^\h+|\h+$|(\h)+/\1/g' file "foo foo" (bar bar) (19) "foo foo"


2

Try this: #! /bin/bash sleep 2 while feh --cycle-once -zD $1 *.png; do :; done This way, the cycle will end when feh exits with a nonzero status (as it does when you terminate it).



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