Tag Info

New answers tagged

-1

There are various tools that can do this. If you have pcregrep (it should be available in your distribution's repository), you can do: $ pcregrep -M 'X\n[^\n]+Y' file 31 X 32 Y 33 X 34 Y 36 X 37 Y The -M switch allows pattern to match across newline characters and the regular expression matches an X, followed by a newline, then any non-newline characters ...


1

If I understand you correctly, you only want lines containing X if they are then followed by a line containing Y, and then you want both lines. grep -A1 X filename |grep --no-group-separator -B1 Y


-1

The command cat -n [filename] | tail will get a quick count and display of the most recent records if that's what you're looking for. The -f switch makes it persistent until escape - which really doesn't sound applicable in your scenario or is redundant. wc -l [filename] will get a count of the lines in the target wc -l [filenameprefix]* will count all ...


1

With sed: sed -e '$!N;/\n<name>/s/\n//' file If all your lines are in a sequence, you can use paste: paste -sd'\0\n' file


0

sed is the more simple and efficient tool for this editor grade modification: sed 's-,-ctrlVtab-g' customers You can also very easily achieve the same modification with ex, vi or emacs.


6

don's might be better in most cases, but just in case the file is really big, and you can't get sed to handle a script file that large (which can happen at around 5000+ lines of script), here it is with plain sed: sed -ne:t -e"/\n.*$match/D" \ -e'$!N;//D;/'"$match/{" \ -e"s/\n/&/$A;t" \ -e'$q;bt' -e\} \ ...


-1

Two methods using sed: sed -i "s|testing.db.com/||g" file_path_here or echo "testing.db.com/7fad416d-f2b3-4259-b98d-2449957a3123" | sed -e "s|testing.db.com/||g"


0

Using Perl: < file.txt perl -pe 's/.*\///'


3

Using cut : $ cut -sd'/' -f2 file.txt ##This will print only the lines containing / 7fad416d-f2b3-4259-b98d-2449957a3123 8a8589bf-49e3-4cd7-af15-6753067355c6 The following suggestions assumes that / appears only once in a line : Using grep : $ grep -o '[^/]*$' file.txt ##This will print the lines not having / too 7fad416d-f2b3-4259-b98d-2449957a3123 ...


1

How about (using GNU grep and bash): $ grep -vFf - file.txt < <(grep -B2 -A1 'fff' file.txt) aaa bbb ccc hhh iii Here we are finding the lines to be discarded by grep -B2 -A1 'fff' file.txt, then using this as an input file to find the desired lines discarding these.


6

You could use grep with -A and -B to print exactly the parts of the file you want to exclude but add the -n switch to also print the line numbers and then format the output and pass it as a command script to sed to delete those lines: grep -n -A1 -B2 PATTERN infile | \ sed -n 's/^\([0-9]\{1,\}\).*/\1d/p' | \ sed -f - infile Another way with comm: comm ...


4

If you don't mind using vim: $ export PAT=fff A=1 B=2 $ vim -Nes "+g/${PAT}/.-${B},.+${A}d" '+w !tee' '+q!' foo aaa bbb ccc hhh iii -Nes turns on non-compatible, silent ex mode. Useful for scripting. +{command} tell vim to run {command} on the file. g/${PAT}/ - on all lines matching /fff/. This gets tricky if the pattern contains regular expression ...


2

easy enough with sed: echo "testing.db.com/7fad416d-f2b3-4259-b98d-2449957a3123" | sed -e "s/.*\///" basic syntax is "s/search/replace/" and here we search for .*\/ which means everything ending with slash (escaped) and we replace it with nothing.


4

As long as stdout and stderr are line-buffered and the lines of one of the two are always spottable, mixing them is not a problem: consider the output of a program where both stdout and stderr are line-buffered, and where stderr is easily spottable: $ cat file xxxxxxxxxx tar: ---------- yyyyyyyyyy tar: ---------- zzzzzzzzzz tar: ---------- Extracting ...


1

sed -r ':a;N;s!;N/A!!g;s/^(([^;]*;){3})(.*)\n\1/\1\3;/;T;s!^(([^S][^;]*;){3,})(S*SM-[^;]*);(([^S][^;]*;){1,})(.*)!\1\4\3/\6!;ta' inputfile Even though the implied math of your comment for only 6 fields in the output suggests that there are only pairs, this is the looping version with changes to output SM-1-1/SM-1-2 when matched.


1

TXR: @(repeat) @ (cases) @id;@f2;@f3;@val1;@nil;@sm1 @id;@f2;@f3;@val2;@nil;@sm2 @ (do (put-line `@id;@f2;@f3;@val1;@val2;@sm1/@sm2`)) @ (or) @line @ (do (put-line line)) @ (end) @(end) $ txr data.txr data D04005;4;279;0;0;SSM-4-1 D04005;5;40;0;0;SSM-5-1 LE040A;1;363;(26.3);(27.4);SM-1-1/SM-1-2


2

sed ':n s|;N/A;|;|g;$!N s|^\(\([^;]*;\)\{3\}\)\(.*\)\n\1|\1\3;|;tn P;D ' <<\IN D04005;4;279;0;0;SSM-4-1 D04005;5;40;0;0;SSM-5-1 LE040A;1;363;(26.3);N/A;SM-1-1 LE040A;1;363;(27.4);N/A;SM-1-2 LE040A;1;363;(28.5);N/A;SM-1-3 LE040A;1;363;(29.6);N/A;SM-1-4 IN That will continue to branch back to test for every sequential in input, merging ...


5

there is a missing -e before s/foo/bar/ (*) there is a confusion, are you (the script) editing index or www/index.js ? if index is a template file (with API_CONTEXT_URL) to be used to produce www/index.js, I would suggest sed -e s,API_CONTEXT_URL,http://localhost:5557,g index > www/index.js note that you can use any chat a separator between ...


0

In addition to cuonglm's answer: :|paste -sd'\n' $(sed $"s/\s/ - - /g" <<< $(ls -1|paste -sd" " -)) For this to work you need to have all files, that should be concatenated within one folder and at it's best sorted alphanumerical. The output can then be redirectet to a file. Whats happening here? : That's the true function in bash. This ...


1

The following solution is based on nothing but xxd (one of the tools mentioned in the question), Bash and GNU sed. It assumes that the input consists of complete bytes (groups of eight letters), arbitrarily separated by newlines. The approach is: Strip all newlines. Group letters into four-letter groups terminated by spaces. Filter these quadgraphs into ...


1

printf '\n\n' | cat file1.md - file2.md Something like cuonglm's, I think. Aspiring to it, anyway.


4

The following command yields the requested output: cut -d ' ' -f 1,3-10 file1


0

The example you gave has no common lines. I assume that your actual file does. If so, you can do this: awk 'NR==FNR{a[$1][$2]++; next} {if($1 in a){if($2 in a[$1]){print}}}' file2 file1


1

TXR Lisp step by step: $ cat data Echo Alpha Delta Charlie Golf Bravo Hotel Foxtrot $ txr -p '(get-lines)' < data ("Echo" "Alpha" "Delta" "Charlie" "" "Golf" "Bravo" "Hotel" "Foxtrot") $ txr -t '(get-lines)' < data Echo Alpha Delta Charlie Golf Bravo Hotel Foxtrot $ txr -p '(partition* (get-lines) (op where [chain length zerop]))' < data ...


1

I wrote a tool in haskell that allows you to use sort, shuf, tac or any other command on paragraphs of text. https://gist.github.com/siers/01306a361c22f2de0122 It splits the text into blocks, joins the subblocks with \0 char, pipes through the command and finally does the same thing in reverse.


1

Another POSIX answer: paste -d'>\n' /dev/null - - <infile It gets: >Sunshine This is a sunny day. >Darkness A cave is a dark place.


2

POSIXly: sed 's/^/>/;n' < file.in > file.out


0

If you just want to quickly add a newline when processing some pipeline, use this: outputting_program | { cat ; echo ; } it's also POSIX compliant. Then, of course, you can redirect it to a file.


2

paste is the tool for this: sed 's/.*[[:blank:]]//' /proc/filesystems | paste -sd,


2

Using awk awk '{printf "%s%s",(NR>1?",":""),$NF;} END{print""}' /proc/filesystems Using sed sed -r 's/^nodev//; s/^[[:blank:]]*//; H;1h;$!d;x; s/\n/,/g' /proc/filesystems How it works s/^nodev// This eliminates nodev from the beginning of any line s/^[[:blank:]]*// This eliminates the leading whitespace from each line. H This appends the ...


10

Your problem is that by filtering on raw bytes in a UTF-8 character stream, you're eating part of a unicode sequence in a UTF-8 file, resulting in an invalid byte sequence. That can't work. Instead, you need to use a tool that understands UTF-8, and apply a filter on the unicode data, rather than the raw bytes. Since I don't know which implementation of awk ...


1

Using awk you can do it as: awk '{for(i=1; i<=length;i++) if(substr($0,i,1)>="\xS_INDEX" && substr($0,i,1)<="\xE_INDEX"){printf substr($0,i,1);f=1;} if(f)printf "\n"; f=0}' filename Here S_INDEX and E_INDEX are the starting and ending index of ascii in hex. For input: 1243 135 dgfsdaa 125 sdg124 sdf34 Selecting only digits: S_INDEX = ...


0

I use this script to read a file from bottom upward #!/bin/bash echo -n elpmas.file # resulting file ctr_line=0 while read line; do let ctr_line++ tail -n $ctr_line | head -n 1 >> elpmas.file done <sample.file if sample.file contains 1 2 3 the result elpmas.file will be 3 2 1


1

User FloHimself seemed curious about a TXR solution. Here is one using the embedded TXR Lisp: (defvar splits 4) (defvar name "data") (let* ((fi (open-file name "r")) ;; input stream (rc (tuples 4 (get-lines fi))) ;; lazy list of 4-tuples (sz (/ (prop (stat name) :size) splits)) ;; split size (i 1) ...


1

If I understand your question correctly, you want to extract an uploaded file from a requests that consists of: a line consisting of dashes followed by hexadecimal digits; a bunch more non-empty lines (headers); an empty line; the content to extract; a line break; a repeat of the first line; where line breaks are a CRLF sequence, and the content can ...


0

Since this is JSON, I'd use a JSON parser. Python has a JSON library that's very simple to use — just make one function call and you get the data as a Python data structure which you can then iterate over. #!/usr/bin/python import json, sys def explore(tree): if isinstance(tree, list): for child in tree: explore(child) ...


1

Just for fun, perl: perl -MPath::Class -lne ' BEGIN {@codes = map {[split]} file("file2")->slurp; $, = " "} $val = $_; ($code) = grep {$_->[0] <= $val && $val <= $_->[1]} @codes; print $val, $code->[2]; ' file1


5

Another perl: perl -pe 'BEGIN { binmode \*STDOUT } chomp; tr/AB/\0\1/; $_ = pack "B*", $_' Proof: $ echo ABBBAAAABBBBBABBABBBABBB | \ perl -pe 'BEGIN { binmode \*STDOUT } chomp; tr/AB/\0\1/; $_ = pack "B*", $_' | \ od -tx1 0000000 70 fb 77 0000003 The above reads input one line at a time. It's up to you to make sure the lines are exactly what ...


1

Perl: my $len = 24; my $str = "ABBBAAAABBBBBABBABBBABBB\n"; $str =~ s/\s//g; (my $bin = $str) =~ y/AB/01/; my $val = oct("0b".$bin); printf "%s -> %s -> %X\n", $str, $bin, $val; my ($filename, $fh) = ("temp.out"); # write the file open $fh, '>', $filename; print $fh pack("N", $val); # this actually writes 4 bytes close $fh; # now read it, ...


3

I assume he meant aligned columns, not columnar data in general. That's how I would understand the stringently anyway. For example: Bad: 1 200 3 100 3 400 Good: 1 200 3 100 3 400 In other words, make files that are easy for computers to read, not for humans. Adding spaces to align things makes them pretty and easier for you and me to understand ...


3

{ printf '2i[q]sq[?z0=qPl?x]s?l?x' tr -dc AB | tr AB 01 | fold -b24 } <infile | dc In making the following statement, @lcd047 has pretty well nailed my earlier state of confusion: You seem to be confused by the output of od. Use od -tx1 to look at bytes. od -x reads words, and on little endian machines that swaps bytes. I didn't follow ...


0

sed -n 's/65 72 62 65[[:hexdigit:] ]\{1,10\}/&...\n/;/\n/P' You were almost there. You just needed to Print it differently. That command will print from the head of the line up to just beyond the first match on a line for the string specified. If you want to go the other way round, and print just from the head of the first match to the end of a ...


0

This is fairly simple with AWK: File1 into File2 before pattern = "Pointer" First load the contents of File1 into a variable f1="$(<File1)" then do the insertion awk -vf1="$f1" '/Pointer/{print f1;print;next}1' file2 (Or, if you want to insert File1 after "Pointer") awk -vf1="$f1" '/Pointer/{print;print f1;next}1' file2


0

Busybox is highly configurable, so the answer will depend on what was compiled in. Here are some options. catv -v will show nulls as `^@'. split -l 1 will create files xaa xab etc each with one line in it.


2

To print only the matched portion of a string, you can use one of match everything before and after the desired string and replace it with the string: sed 's/.*\(65 72 62 65 .\{1,10\}\).*/\1.../'


3

You can try following awk: awk 'NR == FNR { x[$1] = $1+0; next; } { for (i in x) { if (x[i] > $1+0 && x[i] < $2+0) { print x[i], $3; } } }' file1 file2 Result should be: 4 Alpha 2 Alpha 7 Bravo


0

With a bit of Python, for a change: #!/usr/bin/env python import csv import json import sys from collections import OrderedDict keys = ['IMSI', 'Status', 'ServiceType', 'Number', 'ConnectionType', 'ActivationData'] with open(sys.argv[1], 'rt') as csvfile: reader = csv.reader(csvfile, delimiter='|') for row in reader: print ...


0

tr -d \\n <infile | tr \~ \\n | paste -d~ - - - - - - - - - - - - - - That will work.


2

grep -oP '(?<="title":").*?(?=")' <INPUT_FILE> Explanation grep -oP: Use grep, printing only matches -o using Perl syntax -p. (?<="title":"): Perl lookbehind. Match a string that has "title":" before it. .*?: The actual part to match (and print). Match a "non-greedy" amount of characters before the lookahead (next). (?="): Perl lookahead. ...


0

To do it entirely in the shell: while IFS="|" read -r im st se nu co ac do printf '{ "%s":"%s", "%s":"%s", "%s":"%s", "%s":"%s", "%s":"%s", "%s":"%s" }\n' \ IMSI "$im" \ Status "$st" \ ServiceType "$se" \ Number "$nu" \ ConnectionType "$co" \ ActivationData "$ac" done < ...



Top 50 recent answers are included