Tag Info

New answers tagged

-1

sed -n 's/^ *\([[:upper:]0-9]\{10,\}\).*/\1/p' That will print the first word on a line if the word is comprised of at least 10 consecutive characters that are only uppercase letters and/or numbers. Nothing else is printed. Run on your example data its output is: AVDDPLL1V8 AGNDPLL1V8 DVDDPLL1V1 DGNDPLL1V1


0

If your problem with the existing answers is that they don’t find words that consist only of capital letters (with no digits), then we can adapt Gnouc’s answer like this: awk '$1 ~ /^[[:upper:]0-9]+$/ {print $1}' or awk '$1 ~ /^[[:upper:][:digit:]]+$/ {print $1}' This differs from his solution in that By putting the digits ([0-9] or [:digit:]) into ...


1

It's the 245256667 byte of the file. If you do a: dd if=2001.txt of=error.txt bs=1 count=10 skip=245256667 You should be able to see the non valid utf8 sequence by doing a hexdump -C error.txt


1

Try the below grep command to print all the alphanumeric characters, $ grep -oP '[A-Z0-9]*[A-Z][0-9][A-Z0-9]*' file AVDDPLL1V8 AGNDPLL1V8 DVDDPLL1V1 DGNDPLL1V1


1

On OS X, you can install the needed coreutils via Homebrew: brew install coreutils With this you'll have gsort, which includes the -h command line parameter.


1

One way to do it: $ awk '$1 ~ /^[[:upper:]]+[0-9]+/ {print $1}' file AVDDPLL1V8 AGNDPLL1V8 DVDDPLL1V1 DGNDPLL1V1 Explanation We only check first field of each line, if it start with ^ one or more uppercase characters [[:upper:]]+, follow by one or more numbers [0-9]+, just print it. With your input, I assume that after uppercase characters is one ore ...


4

Simply paste: paste -d'\n' file1.txt file2.txt If you want to use awk, you can: awk '{getline a < "file2.txt";printf "%s\n%s\n", $0, a}' file1.txt


-1

$ cat file | awk -F"," \ '{for ( x=NF ; x>1 ; x-- ) { if ( $x ~ /^Failed/ ) { x = 0 ; print } ; \ if ( $x ~ /^Successful/ ) x = 0 } }' This worked for me.


0

With GNU sed you can just execute cat at whatever point of the script you like. Unlike r - which schedules for output for the end of the line cycle (which can be very frustrating!), e works like i or c in that it immediately writes its output. Here are some examples of its use: printf %s\\n 'these are some words' \ 'that will each appear' \ 'on ...


0

In bash, define a function function hsort { head -n +1 "$1" ; sort <(tail -n +2 "$1"); } then join -a1 -a2 -t, -o0,1.2,1.3,1.4,1.5,1.6,1.7,2.2,2.3,2.4 \ <(join -a1 -a2 -t, -o0,1.2,1.3,1.4,2.2,2.3,2.4 <(hsort file1) <(hsort file2)) \ <(hsort file3) | sed 's/,/, /g' giving N, Mon, Tue, Wed, Mon, Tue, Wed, Mon, Tue, Wed 1, A, B, C, D, E, ...


1

I don't really know what all this is about, but those were some long lines - it was hard to interpret for me. So, I did this: sed 's/,S[^,]*/,A/g;s/,Fa[^,]*/,F/g ' <<\DATA Name,Mon,Tue,Wed,Thu,Fri,Mon Linux,Successfull Fullbackup; Failed Fullbackup,,,,,Failed Fullbackup Unix,,,,Failed Fullbackup,, Windows,Failed Fullbackup,Failed Fullbackup,Failed ...


0

A couple of variations on Stéphane’s answer: (line; grep $'[\t,]Failed[^\t,]*[\t,]*$') < input The (line; grep …) construct comes in very handy; for example, for filtering the output from ps.  If your system doesn’t have the line command, (read x; echo "$x"; grep $'[\t,]Failed[^\t,]*[\t,]*$') < input should work, too.  Alternatively, grep -E ...


0

If you can use perl: $ perl -F',' -anle 'print if $. == 1 or (grep {$_} @F)[-1] =~ /^Failed/' file Name,Mon,Tue,Wed,Thu,Fri,Mon Linux,Successfull Fullbackup; Failed Fullbackup,,,,,Failed Fullbackup Unix,,,,Failed Fullbackup,, MAC,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup,Failed Fullbackup


1

In: sed "s/PATTERN/`cat replacement.txt`/g" "openfile.txt" ' and " should not be a problem. Those are not special to sed. What should be a problem is &, \, / and newline. You can escape those with another sed command: sed "s/PATTERN/$(sed 's@[/\&]@\\&@g;$!s/$/\\/' replacement.txt)/g" openfile.txt Note that it removes the trailing newline ...


1

If you need an awk solution, you could use something like below. awk '/PATTERN/{system("cat file1");next}1' file2 Testing cat file1 ramesh' has " and / in this file and trying "to replace' the contents in /other file. Now, I have file2 as below. cat file2 This is file2 PATTERN After pattern contents go here. PATTERN Now, I use the above ...


2

This should work for you. I did not vote to close as duplicate since you had specified you have ', / and " characters in your file. So, I did the testing as below. I have file1 contents as below. cat file1 ramesh' has " and / in this file and trying "to replace' the contents in /other file. Now, file2 is as below. cat file2 This is file2 PATTERN ...


1

You could try crudini crudini --get file.ini | while read section; do test "$(crudini --get t.ini $section | paste -d, - - -)" = \ 'shortcut,site,theme' || echo error in section $section done


2

Have a look at crudini, which is a shell tool designed for this conf=/etc/puppet/puppet.conf crudini --set "$conf" agent server "$PUPPET_MASTER_TCP_HOST" crudini --set "$conf" agent masterport "$PUPPET_MASTER_TCP_PORT" or a single atomic invocation like: echo " [agent] server=$1 masterport=$2" | crudini --merge /etc/puppet/puppet.conf


0

You can parse the data directly in DB2. If you want to use regular expressions, you use xQuery, and then producte the output that you want. In that way, you do not need to deal with sed.


1

sed would work well here seq 20 | sed '6,10!d' 6 7 8 9 10 You could use this as well: sed -n '6,10p' Or awk, awk '6 <= NR && NR <= 10'


0

I would also add that it is quite easy to do this in pure awk if you, for some reason, wished to not use wc. $ awk 'END { print NR }' /path/to/file The above prints the number of records (NR) present in the file at /path/to/file. Note: unlike wc, this will not print the name of the file. So, if you only wanted the number, this would be a good alternative ...


1

If I understand you correctly, you have a regex pattern in a variable and you would like grep to use it without giving any special meaning to regex metacharacters. If this is the case, the -F (fixed strings) option to grep is what you want: grep -F "$var" your_file Your system may also have a special command (fgrep) that is equivalent to the above: fgrep ...


2

Since this is a large file, it may be worth using a slightly more complex tool for a performance gain. Usually, specialized tools are faster than generalist tools. For example, solving the same problem with cut tends to be faster than grep which tends to be faster than sed which tends to be faster than awk (the flip side being that later tools can do things ...


0

Single-quotes should accomplish what you want. # " is the special charater var='"hello "word";' grep "$var" file From the bash man page: Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash. Enclosing characters ...


1

Why do you want to combine all those files into one file? If you want to feed them to a program that wants stdin, you can do this: cat *.csv | prog or any of the other combination methods mentioned earlier. If the program expects a named file as input, do something like this: mkfifo p cat *.csv > p & prog p If you want one file because one file ...


3

The shell has a built-in variable expansion field separator. So if you have a string and your delimiter is solid you can do: var=32768,'dff0207a-591f-4435-9f8b-7b9b3e6ba2c1','d1f77359b3f7236806489ba3108c771f','NUMBER','US_EN','LATIN','GREEK','GERMAN' ( IFS=,; set -f for field in $var do printf '\n%s\n\t' "$field - md5:" >&2 printf ...


1

With perl: perl -F'\t' -anle 'print if @F == 13' file to edit inplace, add -i option: perl -i.bak -F'\t' -anle 'print if @F == 13' file


2

You almost have it already: awk -F'\t' 'NF==13 {print}' infile > newfile And, if you're on one of those systems where you're charged by the keystroke ( :) ) you can shorten that to awk -F'\t' 'NF==13' infile > newfile


2

There are some good answers here, but I see only one that incorporates the Val part of the problem, and it’s ambiguous whether that is correct.  I agree that awk is “an amazing tool”, but it’s not necessary here; I believe that this sed command: sed -n '/\[part1\]/,/\[part2\]/s/.*Val.*=//p' "$file" probably does what’s desired.  Like the other sed -e ...


1

To get the whole lines from the first part: awk '$1 ~ /^\[/ {n++;next} n==1' To just print the right hand side of the first =: awk '$1 ~ /^\[/ {n++;next} n==1 {sub(/^[^=]*=[[:blank:]]*/,""); print}'


4

you can pipe it to sed: .... | sed -n "/0.1.0/,/1.0.2/p" YourFile


5

You can pipe output to awk: $ ... | awk '/0\.1\.0/,/1\.0\.2/' 0.1.0 0.2.0 1.0.0 1.0.1 1.0.2


0

sed -n '/part2/q;s/[^=]*=//p' \ <<\DATA <Title> [part1] A.File = a A.Val = val1 B.File = a B.Val = val1 [part2] A.File = a1 A.Val = val2 B.File = a B.Val = val1 DATA OUTPUT a val1 a val1 That should do the trick. It will immediately quit the input file the first time it encounters the string part2 anywhere in the input. ...


2

The -i switch causes sed to edit the original file. That means there is no output and since there is no output, your redirection results in an empty file. So, what you want is either sed -i '' 's/$/<@string>/' txt.txt which will change the original txt.txt. Or, just sed 's/$/<@string>/' txt.txt > txt2.txt


2

and use this: sed -n -e '/\[part1\]/,/\[part2\]/p' FILE |sed -e '1d;$d'| awk -F "=" '{print $2}' OUTPUT is: a val1 a val1


4

You need to use a more sophisticated tool to parse the file. For example, awk: #!/bin/sh getCalibDate() { file="${1}" value=$(awk '/\[part/{a++}(a<2 && /Val/){print $NF}' ${file}) for v in $value do echo $v done } getCalibDate ${1} Here, the variable a is incremented each time a line matches [part. Then, the last ...


8

If you have only 4 lines after [part1] you can use -A4 option with grep: cat ${file} | grep -A4 "part1" | cut -d'=' -f2` For general case (more than 4 lines after [part1]) use sed to get the text between two parts: cat ${file} | sed -n "/part1/,/part2/p" | head -n-1 head is to delete additional part2 at the end As terdon said you don't have to use ...


3

There's sed '/_in_/ s/255/1/' which means: for lines matching /_in_/, search for 255 and replace with 1.


2

By default, echo will print a newline character (\n) after the string (which is why you see the shell prompt on the next line instead of the same line where abcd is printed.) You can try adding a -n parameter to echo to disable the trailing newline character. echo -n abcd | wc -c 4


4

If you run echo without the -n option, it writes a newline character after the arguments.  Otherwise, if you typed echo foo, your next shell prompt would appear to the right of the foo.  So wc is counting the newline. How can I prevent echo from printing that? echo -n abcd | wc -c


17

echo print newline (\n) at end line echo abcd | xxd 0000000: 6162 6364 0a abcd. With some echo implementations, you can use -n: -n do not output the trailing newline and test : echo -n abcd | wc -c 4 With some others, you need the \c escape sequence: \c: Suppress the <newline> that otherwise follows the final argument in the ...


-1

cat oldfile.txt |awk '/_in_[0-9]/{sub(255,1)}1' > newfile.txt


6

With GNU sed you can specify line addresses in first~step format so to modify every other line starting with the second you could use address 2~2 e.g. $ sed '2~2 s/(255)/(1)/' file myName_tx_1 VARCHAR(255) myName_in_1 VARCHAR(1) myName_tx_2 VARCHAR(255) myName_in_2 VARCHAR(1) myName_tx_3 VARCHAR(255) myName_in_3 VARCHAR(1) myAddress_tx_1 VARCHAR(255) ...


6

With awk: $ awk 'FNR%2==0{sub(255,1)}1' file myName_tx_1 VARCHAR(255) myName_in_1 VARCHAR(1) myName_tx_2 VARCHAR(255) myName_in_2 VARCHAR(1) myName_tx_3 VARCHAR(255) myName_in_3 VARCHAR(1) myAddress_tx_1 VARCHAR(255) myAddress_in_2 VARCHAR(1) Explanation FNR%2==0 only matches even lines. If line is even, we replace 255 with 1, sub(255,1). 1 is a true ...


0

Use echo with the -n switch: echo -n "blah blah" >> file


1

If you're printing to a terminal anyway, you maybe just want to omit the newline (-n) and have an explicit carriage return instead: echo -en 'hello, dear\r'; sleep 5; echo -en 'world\r'; echo (Note that you'll want to space-pad your strings to overwrite them completely.)


0

If you want to use echo instead of Thushi's solution with printf, have a look at the -e option of the echo command. This allows you to use certain escape sequences, including \b for backspace. Together with the -n option which supresses newlines you can do this: echo -n "Hello" && sleep 10 && echo -n -e "\b\b\b\b\bworld"


3

echo always outputs to its stdout1. echo foo always does a write(1, "foo\n"). When you do echo foo > file, the shell changes the stdout for the echo command to a new file file. echo foo then still does a write(1, "foo\n") but this time its stdout (its file descriptor 1) points to file, not to the stdout it inherits from the shell. If you want to write ...


0

> actually redirects stdout to a file. What you try to do, is in other words to redirect stdout to stdout itself - which doesn't make much sence. You got stdout already - > just tells the shell to put the output into a file instead of to the screen. What you can do, is to redirect the output to virtual-terminal or pseudo-terminal you have access ...



Top 50 recent answers are included