Sundeep
  • Member for 6 years, 9 months
  • Last seen this week
Add value in a field for a given condition in specific column of file
Accepted answer
1 votes

Solution using perl and GNU sed perl -pe 's/^[^|]*\|\K(10\d{6}\|)/9$1/' file.txt sed -r 's/^([^|]*\|)(10[0-9]{6}\|)/\19\2/' file.txt In either case, you can add -i.bkp to change the file inplace ...

View answer
Extract everything before the matching string
1 votes

with grep $ grep -o '^.*//.*//' <<< "hdfs://ussbssn01.us.xyz.com//data/ip/list/usa/" hdfs://ussbssn01.us.xyz.com// $ grep -oP '^.*?//.*?//' <<< "hdfs://ussbssn01.us.xyz.com//data/...

View answer
Removing a line from a file which has only specific character
1 votes

grep -vx '\s*\$\s*' file.txt -x Select only those matches that exactly match the whole line -v Invert the sense of matching, to select non-matching lines \s* zero or more white-space characters \$ ...

View answer
how to get content within a specific text from a file
Accepted answer
1 votes

$ grep -oE '".*"' demo.txt | grep -oE '\w+' | sort -u 02 05 06 0698 07 abc -o Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line -E Interpret ...

View answer
How can I delete a line from a file when the line contains a specific string?
0 votes

Using grep grep -vw '888' infile.csv > outfile.csv If you want to edit inplace, sed -i.bak '/\b888\b/d' infile.csv Edit: To delete lines containing 888 only in 4th column sed -i.bak -r '/^(([...

View answer
Rename multiple files via shell script
Accepted answer
3 votes

bash solution (provided extglob shell option is enabled - see pattern matching manual) for i in 0.* ; do mv "$i" "${i##0.*(0)}" ; done ${i## delete longest match from beginning of i variable 0. ...

View answer
Could not execute a Bash Script if / while
Accepted answer
7 votes

index = $number cannot use spaces around = for variable assignment.. use index=$number or ((index = number)) [ $index => 1 ] I suppose you want to check if index is greater than or equal to 1, use [...

View answer
Transforming short repeated words in columns into numbers
Accepted answer
6 votes

sed -i.bak -r 's/ AA$/ 2/;s/ (AB|BA)$/ 1/;s/ BB$/ 0/' input -i.bak in place editing and create a backup of original file as input.bak -r extended regex syntax s/ AA$/ 2/ replace ending character ...

View answer
splitting the gvim editor window horizontally
Accepted answer
5 votes

From command line: open specified files as horizontal splits gvim -o report.log power.log area.log open specified files as vertical splits gvim -O report.log power.log area.log Within the editor ...

View answer
Command "paste - - - - ", how to specify a big column number
Accepted answer
0 votes

String multiplication trick based on this answer: https://stackoverflow.com/a/5349772/4082052 Use substitution to insert programmable number of dashes paste $(for i in {1..400}; do echo -n '- '; ...

View answer
awk to cut portion of a field and still print entire line
0 votes

with GNU sed sed -r 's/^([^.]+)\.[0-9]+ /\1 /' filename ^([^.]+) capture starting string upto first dot character \.[0-9]+ match dot character followed by more than 1 digit characters and if number ...

View answer
Awk command to get the next line from the matched pattern and crop the string in between two patterns
0 votes

paste -d: <(grep -oP '<dt>\K.*(?=<)' file.html) <(grep -oP '<dd>\K.*(?=<)' file.html) | sed 's/:/ : /' Commodity Orgin : uerb45e001.material.com Commodity Code & Dimension ...

View answer
find and remove files bigger than a specific size and type
Accepted answer
22 votes

find -type f \( -name "*zip" -o -name "*tar" -o -name "*gz" \) -size +1M -delete the \( \) construct allows to group different filename patterns by using -delete option, we can avoid piping and ...

View answer
'find -mtime -1 -print | xargs tar' archives all files from directory ignoring the -mtime -1 argument
2 votes

find $SOURCEDIR -mtime -1 also includes $SOURCEDIR in the output, which needs to be removed before further processing Using grep -vx one can define a particular line to be excluded.. find $...

View answer
Use Diff to Exclude Quote Symbol - "
3 votes

assuming file1 has $BlankValue="TEST"; and file2 has $BLANKValue='TEST'; and you want to ignore both case and quote differences, use diff <(tr 'a-z' 'A-Z' < file1) <(tr "a-z'" "A-Z\"" < ...

View answer
How do I sort on the first column every 4 lines in a file with a key in the second column
1 votes

#!/usr/bin/perl use strict; use warnings; use autodie; use open qw< :encoding(ASCII) >; my $filename = $ARGV[0]; my ($ip_fh, $op_fh); open($ip_fh, "<", $filename); open($op_fh, ">", "$...

View answer
bash script does not compare file names with [ and ] correctly
Accepted answer
1 votes

myfile="My File [Official Awesome File].txt" myfile=$(echo $myfile | sed -r 's/(\[|\])/\\\1/g') find ./ -name "${myfile}" -type f can you check if this works? I'm expecting this'll change value ...

View answer
Command to find a string inside a pattern
3 votes

Using grep, assumes java file name consisting of only alphabets, numbers and _ awk '/Caused/{getline; print}' testError.log | grep -oE '\w+\.java' Using sed for general file name awk '/Caused/{...

View answer
1
6 7 8 9
10