Tag Info

New answers tagged

1

For the record, with zsh, there's the ${^array} operator that turns on brace-like expansion on the elements of the array. So: $ a=(one two three) $ b=('foo '${^a}' bar') $ printf '<%s>\n' $b <foo one bar> <foo two bar> <foo three bar> Search and replace also works: $ printf '<%s>\n' ${a//(#m)*/foo $MATCH bar} <foo one ...


2

p='* "foo ' s=' bar $USER' CATEGORIES=(one two three four) CATEGORIES=("${CATEGORIES[@]/#/$p}") CATEGORIES=("${CATEGORIES[@]/%/$s}") paste <(printf '[%s]\n' "${!CATEGORIES[@]}") \ <(printf '%s\n' "${CATEGORIES[@]}") Output: [0] * "foo one bar $USER [1] * "foo two bar $USER [2] * "foo three bar $USER [3] * "foo four bar $USER ...


3

Depending on what your ultimate aim is, you could use printf: $ a=(1 2 3) $ printf "foo %s bar\n" "${a[@]}" foo 1 bar foo 2 bar foo 3 bar printf re-uses the format string until all the arguments are used up, so it provides an easy way to apply some formatting to a set of strings.


1

cat file: 22 z there's a moose loose in the hoose eg. Set args to: loose moose - read by script as "$@" You can use as many as you like. by=( "$@" ) nl file | sed -nf <(for ((i=0;i<${#by[@]};i++)) ;do echo "s/.*${by[i]}.*/$i\t&/; t p" done; echo "s/^/$i\t&/; :p p") | sort -nk1 -nk2 | ...


2

With sqlite and ORDER BY clause: $sqlite3 <<\EOT CREATE TABLE file(line); .import file.txt file SELECT * FROM file ORDER BY CASE WHEN line LIKE '%USB%' THEN 0 WHEN line LIKE '%Realtek%' THEN 1 ELSE 3 END; EOT 00:10.0 USB controller: VIA Technologies, Inc. VT82xxxxx UHCI USB 1.1 Controller (rev 81) 00:10.1 USB controller: VIA Technologies, ...


5

It sounds like you want a scoring system.  Write a script to assign a score to each line, indicating how early in the output you want to see it.  awk seems well suited to this job.  For your example: #!/bin/sh awk '{score=0} /usb/ {score=1} /Plantronics/ {score=2} {print score, NR, $0}' "$@" This assigns a score of 0 to every line by default, ...


2

What you're doing isn't really sorting, but selecting. So what you'd want to do is first select all the lines that contain "Platronics", then all the lines that contain "usb", and so on, and finally all the lines that don't match anything. I don't know of a command that does this in one go, but you can write a little script using python that does what you ...


1

Here is your script fixed to work the way I think you meant it to work: i=1 while read line; do while :; do var=$( echo $line | cut -d ':' -f$i ) i=$( expr $i + 1 ) [[ "$var" != "" ]] || break echo $var done done <$1 A couple of remarks: i was not ...


1

Use grep with PCRE: $ var='/home/path/archive/logs/path.log-2015-04-13.0.gz:2015-05-13 00:43:49,779 INFO [DEUX-DR-SAMPLE-1] c.i.s.p.DeuxProxyPMMProcessor [DEUX : 361] SVRREQ|dataID|server request: (deliver: (pdu: 0 5 0 282190) (addr: 1 1 adress) (addr: 1 1 mssidn) (sm: enc: ASCII msg: id:dataID stat:pattern)' $ grep -Po '.*?SVRREQ\|\K[^|]+(?=\|)' ...


0

The stuff inside the backticks (it's called command substitution, by the way) is a command to be executed. When you write $var | grep '\.txt$' this executes the value of var as a command. That's not what you want: you want to pass the value as input to the grep command. That's basically echo "$var" | grep '\.txt$' so the assignment would be var=`echo ...


1

You should try moving away from the back tick operators for executing commands. Not as portable as using something along the lines of the following: $ my_var=$(ls -la | grep '^...x') In regards to your problem, try $ var=$(echo $var | grep '\.txt$')


1

Here are a couple of approaches (well, 2½): If you’re interested in being able to incrementally modify PS1 settings that follow a general template, Create a file that contains only the PS1=… line. Write sed commands to edit it, piece by piece; e.g., sed -e 's/${debian_chroot:+($debian_chroot)}/\\d \\t /' sed -e 's/\\033/\\e/g' etc. You might find it ...


0

Assuming a posixy shell (/bin/sh or /bin/bash can do this) all=$(echo *) first=${all%% *} The construct ${all%% *} is an example of substring removal. The %% means delete the longest match of * (a space followed by anything) from the right-hand end of the variable all. You can read more about string manipulation here. This solution assumes that the ...


0

my preference is to anchor to the start of the string to reduce back tracking and I like extended regexp so I would use sed -re 's/^([0-9]{6}) .*( 2015)$/\1\2/' names.txt what this does -r extended regexp -e expression to follow s substitute ^ beginning of line ( start subpattern [0-9] a digit {6} previous occurs exactly six times ) end subexpression . ...


1

You could do sed -i -e 's/[[:space:]]\+.\+2015$/ 2015/' names.txt If you want to save it into the same file. Drop the -i if you just want to print to stdout, which you could redirect into another file. It will match any number of spaces followed by anything up to 2015 at the end of the line, then replace that whole match with " 2015" Another ...



Top 50 recent answers are included