Tag Info

New answers tagged

4

You can use this command: awk '{print $1}' filename > newfile where filename is the name of the original big file and newfileis the file that will get the results.


0

Here is awk solution: For deleting odd line: awk 'NR%2==0{ print $0 > "infile" }' infile For deleting even lines: awk 'NR%2{ print $0 > "infile" }' infile Note that above commands deletes the related lines in-place from infile input file, so be careful you write the output in to the same input file. You can write them into another separate file ...


-1

sed '0~2d' file This command is used to delete the even lines in the files.


1

It all depends on what you want. If you want to process files one after the other, you can simply call your awk script on both files sequentially with a loop, and redirect the output: (for file in outfile1.out outfile2.out; do awk -f awk_file.awk < "$file"; done) > text_file.txt or in this case of two files, simply (awk -f awk_file.awk < ...


-1

Yes. The following sed command first removes all trailing whitespaces (s/ *$//) and then everything up to and including the last whitespace (s/.* //). It is probably worthwhile replacing literal whitespace with [[:blank:]] in order to capture tabs and other space-like characters. $ echo " aaa bbb cc " | sed -e 's/ *$//' -e 's/.* //' cc $ echo " aaa bbb ...


0

If your input lines are the same type, you can do like this: #!/bin/bash LOG="/root/1.txt" echo "Date | Hostname | Threat | DATE+time | Critical/High | Count | --- | External IP | Internal IP | TCP/UDP | Port | External Port | Category | Vulnerability" > 1.csv < $LOG awk '{print $1" "$2" "$3 " | " $4 " | " $5 " | " $6" "$7 " | " $8" "$9" "$10 " | " ...


0

If you qualify word to mean any sequence of 1 or more non-blank characters then the answer is definitely yes, and it is very simply done as well. This is because [[:blank:]]* and [^[:blank:]]* are boolean complements and - provided all characters in a string are complete - [[:blank:]]*U[^[:blank:]]* can describe any possible string in much the same way .* ...


2

You're almost there. Just specify the last word: sed 's/^.* \([^ ][^ ]*\)/\1/g' What it does: '^.* ' deletes everything within the start of the line and any spaces. '\(...)\' matches a pattern and returns it as \1. '[^ ]' matches anything without a space in it. (Edited to add better solution. Thanks Hildred!)


1

You could use some adequate pattern of grep instead of sed, for instance: grep -o "[a-Z0-9]*$" In this example, the [...] contains ranges of characters considered appropriate for a "word" (alphanumerics in this case, other symbols could be added, some of which must be escaped).


4

awk '{print $NF}' sed 's/[[:blank:]]*$//;s/.*[[:blank:]]//' That would still print an empty line for every blank line. To avoid it: awk 'NF{print $NF}' sed 's/[[:blank:]]*$//;s/.*[[:blank:]]//;/./!d'


2

You can try : sed 's/.* //' awk '{print $NF}'


3

The awk variable $NF is the last field of every record; you can use it to print only the last fields of your file like so: awk '{print $NF}' file


1

With awk, it's awk -F '|' -v OFS='|' '{sub(/^.../, "& ", $4); print}' file But that cannot edit in-place, so you have to: t=$(mktemp) awk -F '|' -v OFS='|' '{sub(/^.../, "& ", $4); print}' file > "$t" && mv "$t" file With sed, sed -i 's/^[^|]*|[^|]*|[^|]*|.../& /' file If you want to validate the postal code, then sed -i ...


4

Yes, you can do it in bash but I have no idea why you would want to. Here's a pure bash solution: $ while read -r mon day time host threat date time crit count sugg out exip \ in inip tcp port export cat vuln; do printf "%s | " "$mon $day $time" "$host" "$threat" "$date $time" \ "$crit $count $sugg" "$out $exip" ...


1

If you think that you can cause the event by a specific action or interaction, by far the simplest method is something like: watch -d -n1 "stty -F /dev/pts/106 -a | grep -Eo '.(icanon|ixon)'" Run this on a new terminal, the option to -F is the terminal you will run the program on (run tty to see what it is before starting it). Omit | grep .. if you want ...


0

Substitute "Some...\n...Thing" by the contents of file "new" in one or more input files perl -i -p0e 's/Some.*?thing\n/`cat new`/se' input.txt ... -i to change input.txt directly -p0 slurp input file file and print it in the end s/regexp/.../s in regexp . is .|\n s/.../exp/e replace by eval(exp) new -- a file containing the replacement text (This ...


0

Not to strong (because don't chech second string but it easy to settle) and can be is not posix compilant but very simple: sed '/^Some text/{:1;/another thing$/!{N;b 1} s/.*/this is completely\ndifferent text/g}' input.txt First command add lines from Some text until have met another thing then second line change it to other text. NOTE Limitation is ...


2

sed -e :n -e '$!N;/\n.*\n/!{$!bn }; s/some text,\n* *something else\n* *another thing/this is completely\ different text/;P;D' <infile I fear you're going to have a difficult time coming up with a solution that suits you until you hammer out a concrete description of the problem - but that's what QA is best suited for, as I see it. Maybe this will give ...


1

This will get you started. Requires GNU awk for the time functions: gawk -F, ' function totime(ymd) {gsub(/[-_]/," ",ymd); return mktime(ymd " 0 0 0")} BEGIN {now = systime(); m1 = now - 86400 * 30; m6 = now - 86400 * 180} FNR == 1 {next} {t = totime($3)} t > m1 {print "m1", $0; next} t > m6 {print "m6", $0} ' file m6 ...


0

Just play with date command inside awk and redirect output from print to desired files according to certain conditions: awk -F, 'BEGIN{ "date -d\"month ago\" +%s" | getline T1; close("date"); "date -d\"6 months ago\" +%s" | getline T6; close("date")} { "date -d" $3 " +%s" | getline t; close("date"); if(t>T1){print $0>"file2";next} ...


3

sed '/P1/,/P2/!d;/P2/q' ...would do the job portably by deleting all lines which do !not fall within the range, then quitting the first time it encounters the end of the range. It does not fail for P2 preceding P1, and it does not require GNU specific syntax to write simply.


1

awk '{print $1,$2,$3}$4{print $1,$2,$4}' == awk '{print $1,$2,$3;$3="\b"}$4' or awk -v OFS="\t" '$4{$4="\n"$1"\t"$2"\t"$4}1' == awk -v OFS="\t" '$4{sub("^","\n"$1"\t"$2"\t",$4)}1' If you want formatted output you can use either OFS variable by -v OFS='\t' or pipe output via column -t


2

$ awk -v OFS='\t' '{for (i=3;i<=NF;i++)print $1,$2,$i}' file SRR959756.1081725 1 RNU2-54P:112:133:hsa-miR-1246 SRR959756.1162547 1 RNU2-54P:112:133:hsa-miR-1246 SRR959756.128602 2 RNU2-37P:85:108:hsa-miR-877-3p SRR959756.128602 2 RNU2-59P:99:122:hsa-miR-877-3p How it works -v OFS='\t' This sets the ...


3

In sed: sed -n '/P1/,/P2/p; /P2/q' -n suppresses the default printing, and you print lines between the matching address ranges using the p command. Normally this would match both the sections, so you quit (q) when the first P2 matches. This will fail if a P2 comes before P1. To handle that case, try: sed -n '/P1/,/P2/{p; /P2/q}'


3

with awk awk '/P1/{a=1};a;/P2/{exit}' file something P1 something content1 content2 something P2 something


1

Suppose this is usual task for print unique fild in awk awk -F"[| ]+" -v OFS=" |" ' NR==1 { for (i=0;i<length($1);i++) blank=" " blank } { if (($1,$2) in b) $2="" else b[$1,$2]=1 if ($1 in a) $1=blank else a[$1]=1 print }' large.csv


0

I like gawk's FPAT for this awk -vFPAT='[[:digit:]]+\\.' 'NF{$1=$1; print}' file 14. 20. 1. 1. 2. 2. 3. 3.


1

Using a for loop: awk '/^$/{next}; {for(i=1;i<=5;i++){printf "%s\t", $i}; printf "%.6f\n", $6*3}' file Data 9390.900391 10573.089844 80.000000 200.000000 8.100000 Data 17762.810547 18536.189453 85.000000 200.000000 8.100000


2

Try: $ awk 'NF{$NF = sprintf("%.6f", $NF*3)}1' file Data 9390.900391 10573.089844 80.000000 200.000000 8.100000 Data 17762.810547 18536.189453 85.000000 200.000000 8.100000 Change $NF to the $n where nis the field you want to change.


0

The following script used the intermediate substitution to = symbol (you are free to change to any you'l want): #!/bin/sed -f s/\s\+\s/=/g /:[^=:]\+:/s/:[^=:]\+ /&=/g s/^=/\t/ s/ *=$// s/ *=/\n\t/g So ifconfig eth0 | sed -f script.above outputs: eth0 Link encap:Ethernet HWaddr 74:2f:68:8b:b8:6e inet addr:192.168.1.247 ...


1

You can start with sed 's/\(:[^: ]\+\) \([^(]\)/\1\n\2/g;s/\()\)/\1\n/;s/^ \+//' it should be close enough, and most probably can be simplified and optimized further. The result: eth0 Link encap:Ethernet HWaddr CE:FD:75:DF:A5:6D inet addr:172.16.4.177 Bcast:172.16.5.255 Mask:255.255.254.0 inet6 addr: fe80::adfd:75ef:fedf:v56d/64 Scope:Link UP ...


0

The following script should be tested on much bigger volume of data (more than 4 lines) to check correct execution this statement if ((A[1]<$3 && $4<=A[2])||(A[1]<=$3 && $4<A[2])) awk ' BEGIN{SUBSEP="-"} { if (($3, $4) in ids) ids[$3,$4]=ids[$3,$4] "," $1 else ids[$3,$4]=$1 } ...


4

Note that you don't have to read the file beforehand, sed has the r command that can read a file: $ printf -v var "%s\n" "s1random stuff" "s2 more random stuff" "s1 final random stuff" $ echo "$var" s1random stuff s2 more random stuff s1 final random stuff $ sed '/^s2/r file.txt' <<< "$var" s1random stuff s2 more random stuff line 1 line 2 s1 ...


1

You need to replace the newlines in the variable with \newline. And the text to be inserted needs to be preceded by \newline. var=$(<file.txt) # Put backslash before newlines in $var var=${var// /\\ } printf "s1random stuff\ns2 more random stuff\ns1 final random stuff\n" | sed "/^s2/a \ $var"


0

Are you trying to do something like this?: LINE1=`cat test.file | sed '1!d'` LINE2=`cat test.file | sed '2!d'` LINE3=`cat test.file | sed '3!d'` LINE4=`cat test.file | sed '4!d'` echo $LINE1 echo $LINE2


3

You get an error because you're attempting to do arithmetic equality with string values. Here are 2 ways to check whether the elements of dfArray are in dsmArray set -A dfArray / /usr /var /tmp ... set -A dsmArray /home /opt /usr ... for a in "${dfArray[@]}"; do in=false for b in "${dsmArray[@]}"; do if [[ $a == $b ]]; then echo "$a is in ...


2

Decision1: Use GNU sed instead of awk sed -i -e '1 i\line1\nline2' -e '$ a\line3\nline4' ./* Decision2: Use loop for for each file in directory for file in ./* do awk ' BEGIN { print "line1\nline2" } { print $0 } END { print "line3\nline4" } ' "$file" > "$file".tmp && mv -f "$file".tmp "$file" done


6

With GNU awk 4.1 or above: find . -type f -exec awk ' @load "inplace" BEGINFILE { inplace_begin(FILENAME, "") print "line1\nline2" } {print} ENDFILE { print "line3\nline4" inplace_end(FILENAME, "") }' {} +


0

The question is not very clear, but I believe you are looking for the following construct: awk 'BEGIN{FS=OFS=","}NR==FNR{print $1,$2,"\"\"";next}{print "\"\"",$1,$NF}' success.csv error.csv Explanation: first of all at the very beginning we set field separator (FS) and output field separator (OFS) to , we process both files in one go, but we check which ...


1

In the first place: read "Please choose fruit [1-3]:" t ...will not work. It looks like you're trying to provide a prompt string to the shell builtin read, but read interprets its first argument as the name of a variable to assign the value of the line it reads from stdin unless it is handed options. read -p "Please choose fruit [1-3]:" t ...is an ...


5

Use Here String redirection <<< together with Command Substitution $() and don't forget to put double quotes around your variables: a=$(awk "NR==$t" <<< "$c")


1

"You could also use find and sed, but I find that this little line of perl works nicely. perl -pi -w -e 's/search/replace/g;' *.php -e means execute the following line of code. -i means edit in-place -w write warnings -p loop over the input file, printing each line after the script is applied to it. " (Extracted from ...


1

You don't want the value of FS; you want the string which matched FS (which can be different strings on the same line): > awk -F"[0-9]+." '{ printf "line %3d.: ",NR; if (NF==1) { print "No FS match in this line"; next; }; str=$0; for(j=1;j<NF;j++) { match(str, FS); fs_str=substr(str, RSTART, RLENGTH); printf "_%s_, ",fs_str; str=substr(str, ...


0

There are obviously lots of ways to do this - I think I like @aragaer's sed answer best. Here's one that uses purely bash builtins and doesn't need to fork any external utilities: for f in file{1..80}.dat; do { read && read && printf "%s\n" "$REPLY"; } < "$f" done > output.dat


1

aragaer’s sed solution is nicest, yes. But I since I do enjoy a bit of head|tail cutting, have a head|tail solution that supports multiple files, not just a single input.dat. Using a for-loop, instead of passing a list of files to sed, also makes it easier to do other thing with the file before/after extracting the second line with sed. # empty output.dat ...


3

sed would be enough: sed -sn 2p file{1..80}.dat > output.dat -s option is needed to print 2nd line from each file, otherwise only 2nd line of first file will be printed.


0

The parts you do not want have a slash and three characters. The part that you want to keep also starts with a slash and have more than three characters, but the third character is an underscore, so we delete all the parts that look like /XXX but not /XX_ This leaves the leading slash on the part that we want to keep, so we finally also delete that one ...


2

What about ... head -n 2 input.dat | tail -n 1 | awk ...


5

Remove while loop and make use of shell brace expansion and also FNR, a built-in awk variable: awk 'FNR==2{print $0 > "output.dat"}' file{1..80}.dat


0

sed 's|.*/\([^/]*_[^/]*\)/.*|\1| ' <<\INPUT /ABC/RTE/AD_900_VOP_123/OPP /ABC/RTE/TRE/AD_900_VOP_145/BBB /ABC/RTE/AN_900_VFP_124/FBF /ABC/RTE/HD_900_FOP_153/WEW /ABD/RDV/AD_900_VOP_123/OPP /ABC/RTE/WD_900_VOP_123/GRR/TRD /ABC/RTE/RTD/AR_900_VOP_443/SDD INPUT That will remove up to the second to last occurrence of / immediately preceding a _ ...



Top 50 recent answers are included