Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
grep -o '\w\{4\}' file
I want to find the words longer than a value given by the user. Now, the above code gives me the words with at least 4 letters but if I'm replacing 4 with $n after i read a number into n it says that the content between my brackets is incorrect. How could I use a value read instead of a predefined one?
With single quotes, the $n will not be interpreted. It works if you use double quotes:
grep -o "\w\{$n\}" file
But I think you will want to use
grep -o "\w\{$n,\}" file
because else words of length 8-11 will match twice, 12-15 three times etc.
n=5
eval "grep -o '\w\{$n\}' filename"
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I would like to find pattern in a specific line (no case sensitive). And if I find it make something.
I can’t use grep since find in all file.
What is the best method?
Try this, where 2 is the line that you want to search for pattern:
sed '2!d;/pattern/I!d' input
This sed script basically deletes all lines except line 2, as 2! is not line 2; then among all lines that get past that action, which means only line 2 in this case, all those not matching pattern are deleted too.
So all that remains, and is printed by sed implicitly, is line 2 only if it matches pattern.
Note that the flag I for case insensitivity is not POSIX, IIRC. Try it out. If it doesn't work, you can always use [pP][aA][tT][tT][eE][rR][nN].
Also note that this command will return 0 in both the case of matching and not matching. If you need to take action depending on whether the file matched or not, you can do like this
[[ -n $(sed '2!d;/pattern/I!d' input) ]] && echo matched || echo not matched
If the pattern is a variable, then you can include it like this:
sed '2!d;/'"$variable"'/I!d' input
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have some 50 files in D:\dummy\ folder. It has filenames like
Abc_566_1.xml.error
Abc_566_2.xml.error
...
Abc&566_50.xml.error
I want a shell script/solution to remove .error in all 50 filenames.
The shell can do quite an amount of string processing, like chopping off substrings at either end of a string. I usually do jobs like yours with
for file in *.error; do
mv "$file" "${file%.error}"
done
For more on this, read your shell manual, especially the section on parameter expansion.
Give this a try:
for each in *.error ; do
mv $each `echo $each | sed -e 's/\.error$//'`
done
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I've been trying to figure out a way to take replace a specific bash argument with another argument in the same list. For example if I have something like:
/home/text.c /bin/mv {} ~/.trash
I want to replace the {} with the file I have, in this case text.c
So far I have this:
sed -i 's/{}/"$1"/g' "$#"
My assumption was that it would look through the arguments ("$#"), find {} and replace it with the first argument (/home/text.c). However, I only get back:
sed: -e expression #1, char 2: extra characters after command
/bin/mv: cannot stat '{}': No such file or directory
Any help would be greatly appreciated.
You can use BASH string replacement on $* which is string form of all of your arguments:
local args="$*"
echo "${args/\{\}/$1}"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I hope someone can help me. my question is with a script in bash that receives a certain amount of parameters and then show them in the reverse order of the one. as I keep name one for each line in the vvariable and then show them around.
#!/bin/bash
var=""
for i in "$#";do
var+=`echo $i`
done
If you want reversed order, you have to loop in reversed order:
for ((i=$#; i>=1;i--)); do
a=${!i}
echo "$a"
done
The simplest way to achieve this would be:
echo $# | rev
$# stores all the arguments passed, and rev, as its name suggests, reverses the order of characters in the line.
Edit:
After reading your comment, I can suggest the following approach:
for i in `echo $# | rev`; do
j=`echo $i | rev`
echo -n "$j "
done
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I want get a string which is between two characters in bash script. For example;
AAA_Ubuntu-bbb, in this string I want to get "Ubuntu" as string. How I do it?
var=AAA_Ubuntu-bbb
string=${var#*_} # remove before _
string=${string%%-*} # remove after -
echo "$string"
See the section of the bash man page on Parameter Expansion for explanations of these operators.
Assuming the portion of the string you want is enclosed between _ and - and does not contain such characters, cut provides an easy solution. Define a function like this:
function extractName {
cut -d _ -f 2 <<< "$1" | cut -d - -f 1
}
and use it in your code like:
name=$(extractName "AAA_Ubuntu-bbb")
echo $name