How can I remove ALL whitescape in a file, using Notepad++?
Example data:
;; ;;;2017-03-02;8.026944444;16.88583333;8.858888889
;; ; ; ; 2017-03-03 ; 7.912777778 ; 16.88583333 ; 8.973055556
;; ; ; ; 2017-03-06 ; 7.954444444 ; 16.88583333 ; 8.931388889
; ; ; ; ; 2017-03-07 ; 7.926388889 ; 16.88583333 ; 8.959444444
;;;;;2017-03-05;8.984722222;16.98472222 ;8
To delete all spaces in the file, replace ' +' with '' (quotes only for demonstration, please remove them). You need to have the checkbox "Regular expression" checked.
To remove all spaces and tabs, replace '[ \t]+' with '' (remove quotes).
This works for big files, too, where the solution of #Learner will be tiresome.
No need for regular expressions. In Find what add a whitespace character and then replace with nothing.
Press Shift+Alt then press the down button before "56". Then backspace. You will see the cursor becomes big and then you can remove the spaces all at once.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
I'm trying to replace a string over multiple files using the command sed like:
grep -rl matchstring somedir/ | xargs sed -i 's/string1/string2/g'
My question is: is there a way to enable it to ask for confirmation at each substitution?
I tried to include the latter c after g in the substitution command, but it didn't succeed.
Thank you in advance for any comments.
The sed utility does not have confirmation ability. If/Until it is implemented, consider using other tools.
The 'ex' (vim in command line mode), has lot of power, including per change confirmation. The confirmation mode is geared toward visual usage. One problem is that once a single change is not approved, the substitution stop. On the surface, this is not what is being asked.
Potentially solution will be to use scripting engine capable of substitutions, and implement a confirmation logic. Awk, Perl, python meet those requirements.
Perl has edit in place option. Script can be invoked with 'perl -i.bak'.
Security: Notice code may need additional protection against injection into RE. See below
Activate:
grep -rl matchstring somedir/ | xargs perl -i.bak sub-conf.pl 'string1' 'string2'
#! /usr/bin/perl
use strict ;
my ($in_pattern, $replacement) = (shift, shift) ;
# Convert string to regexp, disable special characters, etc.
my $pattern = quotemeta($in_pattern) ;
open TTY, "/dev/tty" or die "Failed to open TTY: $!" ;
# Read a line into orig
my $eof ;
while ( my $orig = <> ) {
my $new = $orig =~ s/$pattern/$replacement/gr ;
if ( $new ne $orig ) {
print STDERR "Replace line ${.}:\n< ${orig}> $new" ;
while (1) {
print STDERR "(Y/N) ?" ;
my $yn = <TTY> ;
unless ( defined($yn) ) { $eof = 1 ; last } ;
if ( $yn =~ /[Yy]/ ) { print $new ; last ; } ;
if ( $yn =~ /[Nn]/ ) { print $orig ; last ; } ;
} ;
} else {
print $orig ;
} ;
die 'EOF' if $eof ;
} ;
See: How can I safely validate an untrusted regex in Perl? for explanation of the injection protection in case command need to be extended to accept REGEX.
Please help to achieve this. I added these two properties from non facets index property to facet index property in solr.implex file. Run the full indexer ,
added customer review for some of the products and approved the status as well in spite of that these two properties are not showing as facets in storefront.
Please guide me step by step how can I achieve this. I have already wasted 2.5 days to achieve the same.
I am using hybris 6.4
First, run the below Impex form the HAC. Don't forget to change apparel-ukProductType with your Indexed type.
$solrIndexedType=apparel-ukProductType
# Facet properties
INSERT_UPDATE SolrIndexedProperty ; solrIndexedType(identifier)[unique=true] ; name[unique=true] ; type(code) ; sortableType(code) ; currency[default=false] ; localized[default=false] ; multiValue[default=false] ; useForSpellchecking[default=false] ; useForAutocomplete[default=false] ; fieldValueProvider ; valueProviderParameters[map-delimiter=|] ; ftsPhraseQuery[default=false] ; ftsPhraseQueryBoost ; ftsQuery[default=false] ; ftsQueryBoost ; ftsFuzzyQuery[default=false] ; ftsFuzzyQueryBoost ; ftsWildcardQuery[default=false] ; ftsWildcardQueryType(code)[default=POSTFIX] ; ftsWildcardQueryBoost ; ftsWildcardQueryMinTermLength ; facetType(code) ; facetSort(code) ; priority ; visible ; facet[default=true]
; $solrIndexedType ; reviewAvgRating ; double ; ; ; TRUE ; ; ; ; productReviewAverageRatingValueProvider ; ; ; ; ; ; ; ; ; ; ; ; MultiSelectOr ; Custom ; 10000 ; true ;
; $solrIndexedType ; inStockFlag ; boolean ; ; ; ; ; ; ; productInStockFlagValueProvider ; ; ; ; ; ; ; ; ; ; ; ; MultiSelectOr ; Custom ; 10000 ; true ;
You can do the same changes from backoffice/hmc.
Go to System > Facet Search > Indexed Type > select your type from list > Find the inStockFlag under Properties filed > scroll to end (right) to edit inStockFlag properties > In popup go to facet setting tab > Mark Facet to true and edit other fields.
Lastly, run the full indexer.
Say I want to search for "ERROR" within a bunch of log files.
I want to print one line for every file that contains "ERROR".
In each line, I want to print the log file path on the left-most edge while the number of "ERROR" on the right-most edge.
I tried using:
printf "%-50s %d" $filePath $errorNumber
...but it's not perfect, since the black console can vary greatly, and the file path sometimes can be quite long.
Just for the pleasure of the eyes, but I am simply incapable of doing so.
Can anyone help me to solve this problem?
Using bash and printf:
printf "%-$(( COLUMNS - ${#errorNumber} ))s%s" \
"$filePath" "$errorNumber"
How it works:
$COLUMNS is the shell's terminal width.
printf does left alignment by putting a - after the %. So printf "%-25s%s\n" foo bar prints "foo", then 22 spaces, then "bar".
bash uses the # as a parameter length variable prefix, so if x=foo, then ${#x} is 3.
Fancy version, suppose the two variables are longer than will fit in one column; if so print them on as many lines as are needed:
printf "%-$(( COLUMNS * ( 1 + ( ${#filePath} + ${#errorNumber} ) / COLUMNS ) \
- ${#errorNumber} ))s%s" "$filePath" "$errorNumber"
Generalized to a function. Syntax is printfLR foo bar, or printfLR < file:
printfLR() { if [ "$1" ] ; then echo "$#" ; else cat ; fi |
while read l r ; do
printf "%-$(( ( 1 + ( ${#l} + ${#r} ) / COLUMNS ) \
* COLUMNS - ${#r} ))s%s" "$l" "$r"
done ; }
Test with:
# command line args
printfLR foo bar
# stdin
fortune | tr -s ' \t' '\n\n' | paste - - | printfLR
I would like to know how to include -, *, and ,/, in the following while loop in addition to the + I have already included. If the user enters something other than +, -, * or / I want the invalid input message to print. However, so far I have only worked out how to include one of the arguments in the code, in this case the +. How do I include the other 3 arguments in the same bit of code? I am a noobie I admit, and I don't currently have the vocabulary to search an answer specific to my needs so thought my best best was writing out the issue.
Any help appreciated. Thanks
echo "Please enter an operation of arithmetic. Press either +, -, * or /"
read operation
while [ $operation != "+" ]; do
echo "sorry, that is an invalid input- re-enter operation of arithmatic"
read operation
You can use that in a while loop like this:
while read -p "Please enter an operation of arithmetic. Press either +, -, * or /: " op &&
[[ $op != [-+/*] ]]; do
echo "sorry, that is an invalid input- re-enter operation of arithmatic"
done
You probably want select here:
PS3="Please enter an operation of arithmetic: "
select op in + - / '*'; do
case $op in
-) echo subtract something ; break ;;
+) echo add something ; break ;;
/) echo divide something ; break ;;
\*) echo multiply something ; break ;;
esac
done
The purpose of the program is to make comments in the file begin in the same column.
if a line begins with ; then it doesn't change
if a line begins with code then ; the program should insert space before ; so it will start in the same column with the farthest ;
for example:
Before:
; Also change "-f elf " for "-f elf64" in build command.
;
section .data ; section for initialized data
str: db 'Hello world!', 0Ah ; message string with new-line char
; at the end (10 decimal)
After:
; Also change "-f elf " for "-f elf64" in build command. # These two line don't change
; # because they start with ;
section .data ; section for initialized data
str: db 'Hello world!', 0Ah ; message string with new-line char
; at the end (10 decimal)
I am a beginner in Linux and shell, so far I have got
echo "Enter the filename"
read name
cat $name | while read line;
do ....
Our teacher told us that we should use two while loop;
Record the longest length before; in the first loop and do the changes in the second while loop.
for now I don't know how to use awk or sed to find the longest length before;
Any ideas?
Here is the solution, assuming that comments in your file begin with the first semi-colon (;) that is not inside a string:
$ cat tst.awk
BEGIN{ ARGV[ARGC] = ARGV[ARGC-1]; ARGC++ }
{
nostrings = ""
tail = $0
while ( match(tail,/'[^']*'/) ) {
nostrings = nostrings substr(tail,1,RSTART-1) sprintf("%*s",RLENGTH,"")
tail = substr(tail,RSTART+RLENGTH)
}
nostrings = nostrings tail
cur = index(nostrings,";")
}
NR==FNR { max = (cur > max ? cur : max); next }
cur > 1 { $0 = sprintf("%-*s%s", max-1, substr($0,1,cur-1), substr($0,cur)) }
{ print }
.
$ awk -f tst.awk file
; Also change "-f elf " for "-f elf64" in build command.
;
section .data ; section for initialized data
str: db 'Hello; world!', 0Ah ; message string with new-line char
; at the end (10 decimal)
and below is how you get to it from a naive starting point (I added a semi-colon inside your Hello World! string for testing - make sure to verify all suggested solutions using that).
Note that the above DOES contain 2 loops on the input as your teacher suggests, but you do not need to manually write them as awk provides the loops for you each time it reads the file. If your input file contains tabs or similar then you need to remove them in advance, e.g. by using pr -e -t.
Here is how you get to the above:
If you cannot have semi-colons in other contexts than as the start of comments then all you need is:
$ cat tst.awk
{ cur = index($0,";") }
NR==FNR { max = (cur > max ? cur : max); next }
cur > 1 { $0 = sprintf("%-*s%s", max-1, substr($0,1,cur-1), substr($0,cur)) }
{ print }
which you'd execute as awk -f tst.awk file file (yes, specify your input file twice).
If your code can contain semi-colons in contexts that are not the start of a comment, e.g. in the middle of a string, then you need to tell us how we can identify semi-colons in comment-start vs other contexts but if it can ONLY appear between singe quotes in strings, e.g. the ; inside 'Hello; World!' below:
$ cat file
; Also change "-f elf " for "-f elf64" in build command.
;
section .data ; section for initialized data
str: db 'Hello; world!', 0Ah ; message string with new-line char
; at the end (10 decimal)
then this is all you need to replace every string with a series of blank chars before finding the first semi-colon (which is then presumably the start of a comment):
$ cat tst.awk
{
nostrings = ""
tail = $0
while ( match(tail,/'[^']*'/) ) {
nostrings = nostrings substr(tail,1,RSTART-1) sprintf("%*s",RLENGTH,"")
tail = substr(tail,RSTART+RLENGTH)
}
nostrings = nostrings tail
cur = index(nostrings,";")
}
...the rest as before...
and finally if you don't want to specify the file name twice on the command line, just duplicate it's name in the ARGV[] array by adding this line at the top:
BEGIN{ ARGV[ARGC] = ARGV[ARGC-1]; ARGC++ }
There are a few printf tricks that make this a manageable project. Take a look at the following. The script formats the assembly file with the assembly code beginning at column 0 to code_width - 1 with the comments following at column code_width lined up after the code. The script is fairly well commented so you should be able to follow along.
The usage is:
bash nameofscript.sh input_file [code_width (default 46char)]
or if you make nameofscript.sh executable, then simply:
./nameofscript.sh input_file [code_width (default 46char)]
NOTE: this script requires Bash, if not run on bash, you may experience inconsistent results. If you have multiple embedded ; in each line, the first will be considered the beginning of a comment. Let me know if you have questions.
#!/bin/bash
## basic function to trim (or stip) the leading & trailing whitespace from a variable
# passed to the fuction. Usage: VAR=$(trimws $VAR)
function trimws {
[ -z "$1" ] && return 1
local strln="${#1}"
[ "$strln" -lt 2 ] && return 1
local trimstr=$1
trimstr="${trimstr#"${trimstr%%[![:space:]]*}"}" # remove leading whitespace characters
trimstr="${trimstr%"${trimstr##*[![:space:]]}"}" # remove trailing whitespace characters
printf "%s" "$trimstr"
return 0
}
afn="$1" # input assembly filename
cwidth=${2:--46} # code field width (- is left justified)
[ "${cwidth:0:1}" = '-' ] || cwidth=-${cwidth} # make sure first char is '-'
[ -r "$afn" ] || { # validate input file is readable
printf "error: file not found: '%s'. Usage: %s <filename> [code_width (46 ch)]\n" "$afn" "${0//\//}"
exit 1
}
## loop through file splitting on ';'
while IFS=$';\n' read -r code comment || [ -n "$comment" ]; do
[ -n "$code" ] || { # if no '$code' comment only line
if [ -n "$comment" ]; then
printf ";%s\n" "$comment" # output the line unchanged
else
printf "\n" # it was a blank line to begin with
fi
continue # read next line
}
code=$(trimws "$code") # trim leading and trailing whitespace
comment=$(trimws "$comment") # same
printf "%*s ; %s\n" "$cwidth" "$code" "$comment" # output new format
done <"$afn"
exit 0
input:
$ cat dat/asmfile.txt
; Also change "-f elf " for "-f elf64" in build command.
;
section .data ; section for initialized data
str: db 'Hello world!', 0Ah ; message string with new-line char
; at the end (10 decimal)
output:
$ bash fmtasmcmt.sh
; Also change "-f elf " for "-f elf64" in build command.
;
section .data ; section for initialized data
str: db 'Hello world!', 0Ah ; message string with new-line char
; at the end (10 decimal)
So yeah, use a while loop to find the longest length, given your input in the local file input:
length=0
length2=0
while IFS= read -r -- i; do
(( ${#i} > length2 )) && length2=${#i}
i=${i/\;*/}
(( ${#i} > length )) && length=${#i}
done < ./input
(( length++ )); (( length2++ ))
In your next while loop, detect whether the line starts with ; using [[ ${i:0:1} = ';' ]] and output it, or format the output with awk using the length you determined: awk -F\; -v len=$length '{ printf "%-"len"s %-40s\n", $1, $2}'. Check here (http://www.unix.com/shell-programming-scripting/117543-formatting-output-columns.html) for more info on column formatting.
Edit: In case you didn't figure it out, the second loop looks like:
while IFS= read -r -- i; do
# echo the original if the line starts with ';'
[[ ${i:0:1} = ';' ]] && echo "$i" && continue
# column formatting with awk
(echo "$i" | grep -q ';') && echo "$i" | awk -v len=$length -v len2=$length2 -F\; '{printf "%-"len"s %-"len2"s\n",$1,";"$2}' || echo "$i"
done < ./input
That will give you what you want for the output.
I think I'm going to use this example for my personal formatting!
#!/usr/bin/perl -s -0
use strict;
our ($com); # command line option
$com = ";" unless defined $com ;
my $max=0;
$_= <>; # slurp file
while( /\n(.+?)$com/g ){
$max=length($1) if length($1) > $max }
s/\n(.+?)$com/sprintf("\n%-$max"."s$com",$1)/ge;
print $_; # print file
usage: align_coms input (after chmod+install)
Options: -com=... to redefine comments (default = ; )
and you can try align_coms -com=# align_coms to align this scripts perl comments :)
Edit 1:
Please see the (wise) comment of #EdMorton about problems when the input has strings (or similar) containing comment starters.
Edit 2: The following version can deal with 'alo; word' "alo; word". It is still
not safe -- real languages have always some extra detail (ex '...\'...', multiline comments) but it is a little bit more robust...
#!/usr/bin/perl -s -0
use strict;
our ($com); # command line option
$com = ";" unless defined $com ;
my $nc=qr{ # no comment regex
( '[^'\n]*' # '....'
| "[^"\n]*" # "...."
| . # common chars
)+?
}x;
my $max=0;
$_= <>; # slurp file
while( /\n($nc)$com/g ){
$max=length($1) if length($1) > $max }
s/\n($nc)$com/sprintf("\n%-$max"."s$com",$1)/ge;
print $_; # print file