Tag Info

Hot answers tagged

13

What are my options? Is there another path with the same "run from anywhere" capability, which I can access without sudo, or another way to achieve something equivalent? How to do it? Create some dir in your home to hold your scripts normally named as bin as convention. mkdir ~/bin Now move your scripts to bin mv somescript ~/bin Now how to ...


8

Using rename (as per heemayl's suggestion I narrowed down the globbing only to filenames ending exactly with ..txt): rename -n 's/(.*)\./$1/' *..txt This will match the filename until the last dot and replace the match with everything but the last dot. If the result is the expected one, remove the -n option: rename 's/(.*)\./$1/' *..txt


7

$ sed 's/@.*[.]/@/' file foobar@org john.smith@com chunkylover69@dk qwerty@uk The regex @.*[.] captures everything from @ to a the last period . on the line. We replace that with @.


7

Using awk: awk -v RS='"' -v ORS='"' 'NR==1{print} NR==2{print; printf"\n";exit}' file This sets the record separator to ". So, we want to print the first two records and then we are done. In more detail: -v RS='"' This sets the input record separator to a double quote. -v ORS='"' This sets the out record separator to a double quote. NR==1{print} ...


6

Using grep: mapfile -t var < <(grep -Po '^\s+\K[^ ]+(?= ?\(\d+\),?$)' file.txt) grep -P will use PCRE (Perl Compatible Regular Expression) grep -o will print the matched portion of the line ^\s+\K will match the lines starting with whitespaces and \K will discard the match [^ ]+ will match our desired portion (?= *\(\d+\)) is the zero width ...


5

Using Perl: < infile perl -0777 -pe 's/((.*?"){2}).*/$1/s' > outfile -0777: slurps the whole file at once instead of one line at the time -p: places a while (<>) {[...]} loop around the script and prints the processed file -e: reads the script from the arguments Perl command breakdown: s: asserts to perform a substitution /: starts the ...


4

Here's a shorter awk version: awk '/TRAP-TYPE/,/[[:alpha:]]*"$/ ' $ awk '/TRAP-TYPE/,/[[:alpha:]]*"$/ ' testfile.txt sCellEventTrap-03-28 TRAP-TYPE -- CAC Code: 00 ENTERPRISE compaq VARIABLES { scellNameDateTime, scellSWComponent, scellECode, ...


4

Kill the parent process of job1 with SIGKILL. Show the processes in a process tree, eg. with htop or ps axf and kill the parent process of job1 and not job1 itself. Install htop with sudo apt-get install htop Downvoters, understand the answer before downvoting


4

In addition to http://askubuntu.com/a/643030/218015 you might can also define an alias inside your .bashrc for small, often used tasks. E.g. alias ll='ls -l' alias ls='ls --color=auto' will create you a "command" ll, which is doing ls -l and ls will be coloured after defining the alias. https://wiki.ubuntuusers.de/alias is having some more examples and a ...


4

If you are certain that your zip-archive contains only one file you can extract it to a different filename with unzip -p > thenewFile Explanation: -p tells unzip to extract the content directly to stdout which then gets redirected into a new file.


4

Another rename variant: rename 's/\Q.././' *..txt using \Q avoids escaping the dots (See http://perldoc.perl.org/perlretut.html)


4

I have a working solution, still working for better one: for f in *; do mv $f ${f%.txt}txt; done Thank to @heemayl note: Rather than using *, use *..txt to get the files as only * will cause problem when you have any file without . and with only one So becomes: for f in *..txt; do mv $f ${f%.txt}txt; done


4

You don't need popen - popen is what you use to start a process. We don't need to start another process. And you don't need to be piping things to/from STDIN/STDOUT ; /proc/meminfo follows the same rules as everything else in *nix. Everything is a file You can just open /proc/meminfo and process it like you would any other file. f = ...


4

You need a double backslash \\ because the single backslash is not only the regex escape character but also the one your shell uses. E.g. you escape the dot, which on shell level just interprets to a regular dot, that is then passed to apt-get and machtes every character (as a regular dot usually does). So the answer is, first the string is interpreted by ...


3

The problem is that as you are using no quoting, you need to escape both the shell and the regular expression interpreter both to interpret . as literal. That's the reason why quoting is important. Your first pattern would work if you just use quotes around it : apt-file search --regexp '.*ssl.*\.so.*' The main thing to note that our final goal is to let ...


3

First notice that it's better to use adduser and not useradd. What is the difference between adduser and useradd? http://unix.stackexchange.com/questions/121071/what-does-adduser-do-that-useradd-doesnt Now back to your command: You should run in the following mannar: sudo useradd -m -c "Samwise the Brave" sam -s /bin/bash man useradd -s, --shell ...


3

You can use capture groups and backreferences e.g. rename -vn -- 's/\.(pdb)\.(\d+)/_$2.$1/' *.pdb.* to match a literal period \. followed by (literal) string pdb, followed by a second period \. and then a sequence of one or more digits \d+, copying the string and digit sequence into numbered capture groups $1 and $2 respectively and then re-substituting ...


3

Using find and rename: find . -depth -type d -not -name '.' -exec rename -n 's/(.*)/$1_foo/' {} + This will add suffix _foo to all directories recursively, -n will show the names of the directories that will be changed. If you are satisfied with the names you can remove the -n option to let the action take place: find . -depth -type d -not -name '.' ...


3

No problems here! The moonshadows@moonshadows-A740GM-M:~$ part is where you can enter your commands, so whenever it comes up, you know the previous process has finished. So you can safely exit the terminal and playonlinux will work fine.


3

Using: find /home/things/backups -mtime +0 -exec \ sh -c 'echo "$0" >> /home/things/logs/backup.log; echo "Hourly rotate successful: $(date). $0 was deleted." >> /home/things/logs/backup.log;' {} \; We start a new shell sh with a command -c and the parameter {} (the find result). In the command we use this parameter via $0.


3

awk method: ARRAY=($(awk 'BEGIN{ORS=" "} /SYNTAX/ && /INTEGER/,/MAX-ACCESS/ {gsub(/SYNTAX|INTEGER|MAX-ACCESS.*|[[:blank:]]|{|}/,"");gsub(/\(/," (");for (i=1;i<=NR;i++) {if ($i~/\(|\)/) $i="" };print}' testfile.txt)) Explanation: We set up ARRAY=(…) structure, with command substitution $(…) within it to catch output of inner awk command into ...


3

You used vovar outside single quotes for the first time, but not the second time: ... awk '/'"$vovar"'/,/}/ {gsub("$vovar"," ") ... If you'd been consistent: $ awk '/'"$vovar"'/,/}/ {gsub("'"$vovar"'"," "); gsub("}",": %s,"); gsub("{"," ");gsub(",",": %s,");print}' foo ciscoFlashCopyStatus : %s: %s,


3

You can grep only the lines with the fields and use sed to remove everything after the opening parenthesis. The resulting list of words can be used directly to populate the array: var=($(grep input-file ' *[a-zA-Z]\+ \?([0-9]\+)' | sed 's/(.*//')) echo ${var[1]} The regular expression used in grep is: space + *: any number of spaces (including 0) ...


3

I think you miss something, When you run two commands seperated by ; you can kill the both commands by the CTRL+c. just to be sure try this command: find / -name a ; ls /home Then kill the above commands with CTRL+c, this would kill both processes, you'll not list your home content. I.e, the next command will not run. UPDATE: This also will be valid ...


3

Here is a little python script: #!/usr/bin/env python2 with open('/path/to/file.txt') as f: print '"'.join(f.read().split('"')[:2]) + '"' f.read().split('"') will read the whole file as a string and then split it on " to get all the " separated portions As we are interested in only the first two " separated portions, '"'.join(f.read().split('"')[:2]) ...


3

Though many programs dont like very long lines as input, when your data is not huge you can often simplify multi-line matching by first manipulating the data to put it all on one line, doing the match, then restoring the newlines. For example, use tr to replace newline \n by some character not in your data (I used carriage-return `\r'), use sed to change ...


2

In python In a one- (long-) liner: python3 -c 'data = [l.split("_") for l in open("f").read().splitlines()]; [print("service_id: "+d[0]+",", "mac_address: "+d[1]) for d in data]' where 'f' is the sourcefile, between (double) quotes. Output: service_id: TBBN027162G mac_adrres: 38d8-2f08-0c40 service_id: TBBN027605G mac_adrres: 38d8-2f08-1518 service_id: ...


2

Using awk to create an SQL query: find . -type f -maxdepth 1 -exec awk -F_ '{print "INSERT INTO table (service_id, mac_adress) VALUES (\""$1"\",\""$2"\");"}' {} \; Example output: INSERT INTO table (service_id, mac_adress) VALUES ("TBBN027162G", "38d8-2f08-0c40"); INSERT INTO table (service_id, mac_adress) VALUES ("TBBN027605G", "38d8-2f08-1518"); INSERT ...


2

I see your problem, you're using single quotes. They won't allow the Bash variable $MYN to expand. You can complicate things by telling awk a new variable, or because it's so simple, you can just switch to double-quotes: awk "NR==$MYN" x I'd have used sed but I can't see any obvious issues with your logic MYN=4 sed "${MYN}q;d" x >> y It seems ...


2

If your goal is to pass a variable to awk in order to use it within the script, you can use awk with the -v option: hey=$(awk -v MYN=$MYN 'NR==MYN' x)



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