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 6 years ago.
Improve this question
I want to insert the content between the matching pattern in shell please help on this
For example :
file called input.txt :
var a = [ ]
file called output.txt :
1152
1185
1185
I want to insert the values from output.txt file to the file input.txt.
it should be like var a = [1152 1185 1185]
sed "s/\[/[ $(xargs < output.txt)/" input.txt
xargs < filename dumps all file lines in just one, i.e., replaces EOL characters for SPACE characters.
$(xargs < filename) expands to the contents of the filename in just one line. Hence the use of double quotes, not single ones.
Related
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'm trying to get contents from a text file into seperate bash variables but I can't get my head around it.
In total 3 variables need to be fetched from the 2nd line.
;File created: 20200727
user.details 184 16 John Smith
Output needs to be:
USERID="184"
GROUPID="16"
FULLNAME="John Smith"
Any ideas? I've tried to seperate it via grep, but as values change thats not an option. Same to awk. Problem I am struggling with is that the last variable can be of any length including spaces, which I previously used as delimiter. The source file can't be changed unfortunately.
Like this with awk:
awk '/user\.details/ {
print "USERID=\""$2"\""
print "GROUPID=\""$3"\""
print "FULLNAME=\""substr($0, index($0,$4))"\""}' file.txt
With bash:
while read -r foo USERID GROUPID FULLNAME; do [[ $foo != ";File" ]] && echo "$USERID $GROUPID $FULLNAME"; done < file
Output:
184 16 John Smith
Variable $FULLNAME contains everything from the third column onwards.
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 3 years ago.
Improve this question
I have an output from a program that I would like to process and If I pipe it to a file I get:
file/path#backup2018
file2/path/more/path/path#backup2019
file3/path#backup2017
And I want to process it so it looks like this:
file/path file.path
file2/path/more/path/path file.path.more.path.path
file3/path file.path
I have figured out how to make it with separate commands but would like a one liner.
$ awk -F# '{s=$1; gsub("/", ".", s); print $1, s}' file | column -t
file/path file.path
file2/path/more/path/path file2.path.more.path.path
file3/path file3.path
using sed
sed 's/\([^#]*\)#.*/\1 \1/g' file|column -t
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 have a text file that contains $string . How would I use sed to create a new txt file without all occurrences of $string ?
You can use the following sed command that will replace all the occurrences of your string variable by the the empty string '' and by redirecting the output to a new output_file.
sed "s/$string//g" input_file > output_file
or you can use the following sed command:
sed -i.bkp "s/$string//g" file
to work directly inside the file passed as parameter, -i.bkp option will create backup file automatically with an extension .bkp, (in the above example file.bkp), if no need of backup, use just -i
Last but not least, your $string variable should not contain any slash / if there are any you should escape all of them using \. You can create a function to perform this task.
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 6 years ago.
Improve this question
Is there a way to change multiple (up to ten) patterns in a single fasta file using sed?
For instance I want to change X for Y:
sed "s/X/Y/g" < file1.fasta > output.fasta
how to add sed "s/\s/_/g" and 8 more commands to the same one-liner?
You can separate the commands by semicolons
sed 's/a/b/;s/c/d/'
(you can also use newlines instead of semicolons)
or you can use multiple -es:
sed -e 's/a/b/' -e 's/c/d/'
see this example: (test with gnu sed):
kent$ echo 'abcd'|sed 's/a/1/;s/b/2/;s/c/3/;s/d/4/'
1234
kent$ echo 'abcd'|sed 'y/abcd/1234/'
1234
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
I want to find a pattern starting from a specific line rather than from the beginning and then delete all the lines starting from this specific position to the point/line where the pattern was first matched.
This will delete starting from line 10 until the pattern is matched:
sed '10,/pattern/d' file > newfile
what about this:
sed -e "$lineno,/$pattern/d" $file
where
$lineno is your line number for start deleting
$pattern is your pattern
$file is your file