Tag Info

New answers tagged

0

On GNU/anything, to insert _after_ the first line: find -name '*.txt' ! -name '*thispattern*' ! -name '*thatpattern*' \ -exec sed -si '1r TheLicense.txt' '{}' + to insert a file before, simplest is somewhat slower and a little bit kludgier: find -name '*.txt' ! -name '*thispattern*' ! -name '*thatpattern*' \ -exec sed -si '1{h;s,.*,cat ...


0

This solution uses bash, find,tac and sed. Copy the following script to a file and make the file executable :chmod +x script Then use as follow : ./script <DIR> <HEADERFILE> Where <DIR> The directory containing files ( or directory containing files..) <HEADERFILE> The file to add on top of each file This is the script: ...


3

The most straight forward way I can see to do this is with GNU find, bash and the sponge utility from moreutils: find dir/with/files -name '*.txt' ! -name '*.DontTouch.txt' -print0 | while IFS= read -rd '' file; do echo 'cat path/to/theCommonComment.txt "$file" | sponge "$file"' done As it stands this will just print the cat/sponge commands ...


1

I would create a file as dummy with the contents that you want to be replaced. So dummy file would look like below. <?php /** * * Copyright (C) MyCompany, Ltd. - All Rights Reserved * Unauthorized copying of this file, via any medium is strictly prohibited * Proprietary and Confidential * * */ After that, I would execute the below ...


3

I'd do this in Perl: perl -lne 'if(/^2009/){$n=$_; next} print "$n $_"' file or, more concisely perl -lne '/^2009/ ? ($n=$_) : print "$n $_"' file The idea is to save the current line ($_ in Perl) as $n if it begins with 2009 and if not, print the current line along with the current value of $n.


2

Using sed: sed -n ' /^2009/ { h } /^2009/ !{ G; s/^\(.*\)\n\(.*\)$/\2 \1/p } ' in_file Explanation as requested: -n - makes sed not print anything unless we tell it to. /^2009/ { h } - when we reach a line beginning with 2009, put it in the hold buffer. /^2009/ !{...} - the pattern inside the {...} is gets applied to every line that doesn't start ...


3

This should probably work awk '/^2009/{Prepend = $0; next} {print Prepend, $0}' file.txt So output looks like: awk '/^2009/{Prepend = $0; next} {print Prepend, $0}' file.txt 2009 150 0 0 0 0000 75.316 0.0400390625 0.00007 0.00000 0.8980 2009 150 0 0 0 0000 76.216 0.0400390625 0.00007 1.00000 0.9046 2009 150 0 0 0 ...


4

You can use awk for this: awk '/^2009/{a=$0;next}{print a" "$0} ' file.txt This will only prepend a space to any lines before the first match of 2009, you can set a default string to prepend like this: awk 'BEGIN{a="My default prepend string";}/^2009/{a=$0;next}{print a" "$0} ' file.txt


0

if the order doesn't matter, you can simply use this: awk '{for(i=2;i<=NF;i++)print $1,$i}' file


0

Awk this awk script will work on an arbitrary number of columns > 2 and order of appearance will be preserved as across then down with no assumptions made about what the columns are (i.e. doesn't matter if they are numeric or not, sorted or not, etc): { for (i = 2; i <= NF; i++) { a[j + i] = $1 " " $i } j += (i - 1); } END { ...


0

Here is an awk solution: $ awk '{a[i++]=$1" "$3;print $1,$2}END{for(i=0;i<length(a);i++){print a[i]}}' file 0 0 0.05 9.6877884e-06 0.1 4.2838688e-05 0.15 0.00016929444 0.2 0.00036426881 0.25 0.00055234582 0.3 0.00077448576 0.35 0.00082546537 0.4 0.0012371619 0.45 0.0013286382 0 0 0.05 0.0024898597 0.1 0.0049595502 0.15 0.0074092494 0.2 0.009839138 0.25 ...


0

Here is another perl solution: $ perl -MTime::Local -nle ' BEGIN { $/ = "--" } ($dd,$mm,$yy) = (localtime)[3..5]; $today = timelocal(0,0,0,$dd,$mm,$yy); $h{$1} = $_ if /Last Date : (.*)$/; END { print $h{$_} for grep { ($d,$m,$y)=split "-",$_; timelocal(0,0,0,$d,$m-1,$y) < $today; } keys %h }' file Here I use timelocal() ...


1

Not quite as elegant as the Perl solution, but the same can be done with Awk with essentially the same algorithm: BEGIN{ FS = " : |\n" RS = "--\n?"; Padding = " 00 00 00"; Today = mktime(strftime("%Y %m %d") Padding); } { Last_date = gensub(/([0-9]{2})-([0-9]{2})-([0-9]{4})/, "\\3 \\2 \\1", "g", $4); if ...


0

As others have mentioned, it's not clear exactly what you want to do, but you can fix the syntax of your approach thus: awk -F, '$2 ~ /^[$*&%#]+$/ { count++ } END {print count}' sample.txt


0

How about this awk -F, '{if ($2 ~ /[^:alnum:]/) l=length($2);print $0" "l}' sample.txt 101,aaa,d01 102,*&%,d02 3 103,$%&,d03 3 104,###,d04 3 Prints each line the count of "special chars", here chars that are not alnum.


1

There are many ways to do this. You don't specify this in your question, but your awk approach is attempting to count those second fields that consist entirely of special characters so that's what my solutions are doing as well. If that's not what you want and instead you want to count the fields that simply contain at least one special character remove the ...


2

One way is with Perl. We read chunks of your input having set the input record seperator to "--" as you data suggests. We reformat your Day-Month-Year date into a form we can numerically, relationally compare; viz. YYYYMMDD. Overall: #!/usr/bin/env perl use strict; use warnings; my @t = localtime(); my $today = sprintf( "%04d%02d%02d", $t[5] + 1900, ...


2

An solution using awk: $ awk -F'=' '$1 ~ /^[ \t]*file.input[ \t]*$/{$2="= /new/name"}{print}' config # file.input = /very/old/name # file.input = /old/name file.input = /new/name file.input.default = /default/name other.file.input = /current/other/name # Other comments other.properties = must stay the same Then you can save it to new config file: awk ...


3

Using the -i option of sed (which edits inplace). The caveat is the character used to delimiter the substitution command must not appear in the NEW string. The example uses ':' rather than '/' to delimit (s:old_string:new_string:) NEW=/new/name sed -i 's:^[ \t]*file.input[ \t]*=\([ \t]*.*\)$:file.input = '${NEW}':' f


1

Some more variations: grep .... file or sed '/..../!d' file or sed -n 's/./&/4p' file or awk 'gsub(/./,"&")>3' file or awk 'length>3' file or GNU awk: awk 'NF>3' FS= file


0

You can use grep: If you count leading spaces in line length: grep -e '[^\ ]\{4,\}' file If you don't count leading spaces in line leangth: grep -e '[^\]\{4,\}' file


1

to directly remove the lines you could: sed -ri '/.{4}/!d' /path/to/file Or BRE: sed -i '/.\{4\}/!d' /path/to/file If a line does not contain 4 or more characters it is deleted. f=/path/to/file cat <<GREP >"$f" $(grep -E ".{4}" "$f") GREP Doing the above in command-substitution subshell will ensure that grep gets a read descriptor on it ...


0

sed '/^.\?.\?.\?$/d' input.txt > output.txt


3

You could use sed. The following would remove lines that are 3 characters long or smaller: sed -r '/^.{,3}$/d' filename In order to save the changes to the file in-place, supply the -i option. If your version of sed doesn't support extended RE syntax, then you could write the same in BRE: sed '/^.\{,3\}$/d' filename which would work with all sed ...


1

Try this (uses GNU split which is default on most Linux distros): ls | sed "s:^:$(pwd)/:" | split -dl 50 --additional-suffix=.txt - /path/to/dest/File


2

ls -1 | split --lines=10 puts the files in the same directory. This can be avoided by ls -1 | (cd /where/ever; split --lines=10) or for a different file name: ls -1 | split --lines=10 /dev/stdin /path/to/splitfile.


1

using awk awk 'NR == FNR{a[$1]=$0; next}; ($1 in a){print a[$1]; next}; {print $1, "-", "-"}' file2 file1 Or slightly terser awk 'NR == FNR{a[$1]=$0; next}; {print $1 in a?a[$1]:$1FS"-"FS"-"}' file2 file1


3

You can use -e to specify multiple commands to run: sed -i -r -e 's/[’'–]/ /g' -e 's/Aβ/ ABeta /g' *.txt Generally: sed [-flags] -e 'command1' -e 'command2' .... -e 'last command' inputfile


1

I wrote a perl tool for exactly that key,value problem: Pairing up the right rows: any number of files. It's also available via GitHub. To execute it, you type: merge -k -e "-" jointest1.txt jointest2.txt


1

awk is probably a good tool to use, since the problem is line-oriented. I'd use this variant of @HaukeLaging's solution, which has less redundancy in the code. Every line that starts with Entry clears a flag, but the header for the particular entry that you want sets the flag. If the flag is set, take the default action of printing the line. awk "/^Entry ...


1

You could also use pcregrep: pcregrep -M '234238.*(\n((?!Entry).)*)*' inputfile This would produce all lines starting from the one containing 234238 until it encounters one that contains the word Entry. For your sample input, it produces: Entry '234238': some text some text some text some text Entry '234238': more text more text more text


2

ENTRY="'234238'" sed -n ':s;/Entry '"$ENTRY"'/{:l;p;n;/^Entry/bs;bl;}' <<\ENTRY Entry '234238': some text some text some text some text Entry '899823': some text some text some text Entry '234238': more text more text more text Entry '645353': some text some text ...


5

awk "/^Entry '234238'/ {printline = 1; print; next} /^Entry / {printline = 0} printline"


1

. 4<<HERE /dev/fd/4 echo "$(sed -rn '/\\\\/{:l;s/([^&]*&.*\()([-0-9.]*)(\).*)/\ "\1$(printf "%.3f" "\2" )\3"/;tl;p;}'<<\SED some words (xyz, abc) & 0.00071 (0.07846) & 0.00411 (-0.13542) \\ some more words (1) & 0.00341 (-0.59991) & 0.00001 (0.99453) \\ SED )" HERE OUTPUT: some words (xyz, ...


3

Awk or Perl would be good tools for this job. Perl is easier thanks to its construct to apply arbitrary code to a regular expression match. perl -pe ' if (s/^([^&]*&)//) { # if there's a &, then strip the prefix… print $1; # and print it s[\((-?[0-9]*\.[0-9]+)\)] # replace decimal numbers in ...


4

If you want that last 10 lines: tail myFile.txt | tr '\n' '\0' | xargs -r0i myCmd {} arg1 arg2 But with GNU xargs, you can also set the delimiter to newline with: tail myFile.txt | xargs -r0i -d '\n' myCmd {} arg1 arg2 (-0 is short for -d '\0'). Portably, you can also simply escape every character: tail myFile.txt | sed 's/./\\&/g' | xargs -I{} ...


1

Here's another bash solution (bash 4): minlen=5 # minimum length of a line mapfile -tO1 < inputfile # Map the file to the array MAPFILE (by default) # Start the array at index 1 for i in "${!MAPFILE[@]}"; do (( ${#MAPFILE[i]} > minlen )) || unset MAPFILE[i] # Remove shorter elements done The resulting array is sparse, so ...


1

Ruby : ruby -lne 'puts $_ if $_.size > 5' intputfile Python : python -c "import sys;[ sys.stdout.write(''.join(line)) for line in sys.stdin if len(line.strip()) > 5 ]" < inputfile


10

With grep: grep -En '.{12}' file For lines at least 12 characters long. With several files: find . -type f -exec grep -En '.{12}' {} + Some grep implementations like GNU grep, can do the file-finding themselves. grep -rEn '.{12}' . But beware of symlinks and other non-regular files.


5

Since the one thing that was missing was a sed solution sed -n '/^.\{6,\}/p' file


5

Bash solution #!/bin/bash count=0 while read; do ((++count)) len=${#REPLY} if ((len > 80)); then echo "Line $count is $len characters." fi done So, e.g., ./whatever.sh < input.file. This does not include the newline by subtracting 1 from $len; if that's not desirable, or your input uses CRLF endings, you should adjust ...


9

AWK solution awk '{ if (length($0) > 5) print $0;'} yourfile Or, more concisely: awk 'length > 5' file


4

With perl (for instance), assuming you are searching for lines longer than 80 characters: To display the lines: $ perl -nle 'print if length > 80' your_file To display the lines number: $ perl -nle 'print "$.\n" if length > 80' your_file Or both: $ perl -nle 'print "[$.]: $_\n" if length > 80' your_file


0

#!/bin/sh #shell basics, POSIX compliant (set -f ;IFS='| ' ; set -- $(cat) ; while [ -n "$3" ] ;do { [ "${t=$2}" != "$2" ] && echo && t=$2 printf '%s|%s|%s\n' "$1" "$2" "$3" ; shift 3 } ; done )<<\SAMPLE 17412193|name1|organization 43979400|name1|organization 1405541|name2|organization 53595498|name2|organization ...


2

You can use following python script to parse that data. Lets assume that you have JSON data from arrays in files like array1.json, array2.json and so on. import json import sys from pprint import pprint jdata = open(sys.argv[1]) data = json.load(jdata) print "InstanceId", " - ", "Name", " - ", "Owner" print data["Instances"][0]["InstanceId"], " - " ...


8

The availability of parsers in nearly every programming language is one of the advantages of JSON as a data-interchange format. Rather than trying to implement a JSON parser, you are likely better off using either a tool built for JSON parsing such as jq or a general purpose script language that has a JSON library. For example, using jq, you could pull out ...


-1

I will now quote extremely liberally (read: copy entirely) from this Stack Overflow answer: You can't parse [X]HTML with regex. Because HTML can't be parsed by regex. Regex is not a tool that can be used to correctly parse HTML. As I have answered in HTML-and-regex questions here so many times before, the use of regex will not allow you to consume HTML. ...


1

I have a quick solution which is useful a lot of times. Notice that is for personal use only(1); it could be refined, added in-place editing, error control, whatever. But I think it's useful as is too. The idea is leveraging LaTeX numbering itself. So, first of all, you need to add labels to your document (which is good nonetheless): ...


-1

In LaTeX this is usually done with \begin{document} \input{section1} \input{section2} \input{section3} \end{document} Where section1 is a input file section1.tex and so on. [Edit] Objection was "not to handle more than one file, because it's too difficult". There are numerous good editors which can handle multiple files grouped in tabs, so to ...


3

A little shorter Perl solution: perl -pe 'print "\n" if ($l =~ /name\d+/ && $_ !~ /$&/);$l=$_;' input if last line ($l) was name\d+ and current line not last match, then print new line assign current line to $l A more general solution perl -pe 'print "\n" if ($l =~ /\|([^\|]+)/ && $_ !~ /$1/);$l=$_;' input



Top 50 recent answers are included