Loop through every 'even' line in a file [closed] - linux

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 10 days ago.
Improve this question
I have a fasta file with the following structure. For context, a fasta file is simply a text file with a header denoted by '>' and below it is the text. I want to create a for-loop that can iterate through every even line of this fasta file.
The name of the file is chicken_topmotifs.fasta
>gene8
ATGAATTATTATACACCTCAAATACTCTCCTCAATCTCTCCAACATTCCCCACCACAATTCTCGGTGACTTTACTACACTTCTACAATCATACACTTCT
>gene12
ATGGTAGATCTCTATTACGATTATCTTTCTTAGATCACATAATTATCACCCCCCCTTATAAATCTACACTTCTACAACCAATTACACTTCTACAAAACA
>gene18
ATGCTTTTACACTTCTACAACTACTTTTAACTCGATACTTCTACAATCTACACATATCACAATAACAAAAACAAAAAGCTACTAATATATATATATACA
>gene21
ATGTCTCAATTTCACCAATCTATAATTTACTACGCCGTACTCTTTATAACCTTACTTTCTTAAATAACATTACACTTCTACATTACATATTTTACATCA
for sequence in chicken_topmotifs.fasta;
do
echo $sequence
done

Just do two reads each time through the loop. The first read gets the odd line, the second one gets the even line after it.
while read -r gene; do
read -r sequence
# do stuff with $sequence
done < chicken_topmotifs.fasta

Assumptions:
ignore header (>) lines
ignore blank lines
One bash idea:
while read -r sequence
do
echo "$sequence"
done < <(grep '^[ATGC]' chicken_topmotifs.fasta)
If we don't have to worry about blank lines:
while read -r sequence
do
echo "$sequence"
done < <(grep -v '^>' chicken_topmotifs.fasta)
Both of these generate:
ATGAATTATTATACACCTCAAATACTCTCCTCAATCTCTCCAACATTCCCCACCACAATTCTCGGTGACTTTACTACACTTCTACAATCATACACTTCT
ATGGTAGATCTCTATTACGATTATCTTTCTTAGATCACATAATTATCACCCCCCCTTATAAATCTACACTTCTACAACCAATTACACTTCTACAAAACA
ATGCTTTTACACTTCTACAACTACTTTTAACTCGATACTTCTACAATCTACACATATCACAATAACAAAAACAAAAAGCTACTAATATATATATATACA
ATGTCTCAATTTCACCAATCTATAATTTACTACGCCGTACTCTTTATAACCTTACTTTCTTAAATAACATTACACTTCTACATTACATATTTTACATCA

Related

Shell command to print the statements with N number of words present in other file [closed]

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 7 months ago.
Improve this question
Suppose I have a file with 3 lines:
output.txt:
Maruti
Zen
Suzuki
I used the command wc -l output.txt to get no. of lines
i got output as 3
Based on the above output I have to execute a command
echo CREATE FROM (sed -n 1p OUTPUT.txt)
echo CREATE FROM (sed -n 2p OUTPUT.txt)
echo CREATE FROM (sed -n 3p OUTPUT.txt)
:
:
echo CREATE FROM (sed -n np OUTPUT.txt)
Can you please suggest a command to replace 1 2 3 .....n in the above command based on the output i get (i.e., no. of lines in my file)
I just gave a sample explanation of my use case. Please suggest a command to execute n no. of times.
You just need one command.
sed 's/^/CREATE FROM /' output.txt
See also Counting lines or enumerating line numbers so I can loop over them - why is this an anti-pattern?

Sort files in a directory by their text character length and copy to other directory [closed]

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 1 year ago.
Improve this question
I'm trying to find the smallest file by character length inside of a directory and, once it is found, I want to rename it and copy it to another directory.
For example, I have two files in one directory ~/Files and these are cars.txt and rabbits.txt
Text in cars.txt:
I like red cars that are big.
Text in rabbits.txt:
I like rabbits.
So far I know how to get the character length of a single file with the command wc -m 'filename' but I don't know how to do it in all the files and sort them in order. I know rabbits.txt is smaller in character length, but how do I compare both of them?
You could sort the files by size, then select the name of the first one:
file=$(wc -m ~/Files/* 2>/dev/null | sort -n | head -n 1 | awk '{print $2}')
echo $file

print multiple words in specific pattern [closed]

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 have a file word.txt
$ cat word.txt
cat
dog
rat
bird
I have a URL like this
https://example.com/?word=
I want to generate a URL list like this
https://example.com/?word=cat
https://example.com/?word=cat,dog
https://example.com/?word=cat,dog,rat
https://example.com/?word=cat,dog,rat,bird
I have 177 words so how can I automate this process with Bash or any other easy programming
Read the input line by line, add the line to the URL. Don't include the comma for the first line.
#! /bin/bash
url='https://example.com/?word='
while read -r line ; do
url+=$comma$line
comma=,
echo "$url"
done < word.txt
This task can be accomplished with a single GNU sed command:
sed -n 's|^|https://example.com/?word=|; :a; p; N; s/\n/,/; ba' word.txt
That should be more efficient than the plain bash.
Explanation:
-n   With this option, sed only produces output when explicitly told to via the p command.
s|^|https://example.com/?word=|   Replaces the beginning of the line with the https://example.com/?word=. This command effectively prepends that string to the pattern space.
:a   Label a for branch command (b). Used when looping through the lines.
p   Prints the pattern space.
N   Adds a newline to the pattern space, then appends the next line of input to the pattern space.
s/\n/,/   Replaces the newline with the comma (,).
ba   Jumps to the label a. This effectively creates a loop for all input lines except the first line.
Another variant:
url='https://example.com/?word='
while read word; do
words+=($word) list="${words[#]}"
printf '%s%s\n' "$url" ${list// /,}
done < word.txt
You can use something like this if you want to do it in python
words=["a","b","c"]
joinedWords = ",".join(words)
url="https://example.com/?word="
print(url,joinedWords)
reqUrl = url+joinedWords
print(reqUrl)

How can i search for an hexadecimal content in a file in a linux/unix/bash script? [closed]

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 an hexadecimal string s and a file f, i need to search the first occurence of that string in the file and save that in a variable with his offset. I thought that the right way to do that is convert the file to hex and search that with a grep. The main problem is that i saw a lot of commands(hexdump,xxd,etc.) to convert but none of them actually work. Any suggestion?
My attempt was like this:
xxd -plain $f > $f
grep "$s" .
output should be like:
> offset:filename
A first approach without any error handling could look like
#!/bin/bash
BINFILE=$1
SEARCHSTRING=$2
HEXSTRING=$(xxd -p ${BINFILE} | tr -d "\n")
echo "${HEXSTRING}"
echo "Searching ${SEARCHSTRING}"
OFFSET=$(grep -aob ${SEARCHSTRING} <<< ${HEXSTRING} | cut -d ":" -f 1)
echo ${OFFSET}:${BINFILE}
I've used xxd here because of Does hexdump respect the endianness of its system?. Please take also note that according How to find a position of a character using grep? grep will return multiple matches, not only the first one. The offset will be counted beginning from 1, not 0. To substract 1 from the variable ${OFFSET} you may use $((${OFFSET}-1)).
I.e. search for the "string" ELF (HEX 454c46) in a system binary will look like
./searchHEX.sh /bin/yes 454c46
7f454c460201010000000000000000000...01000000000000000000000000000000
Searching 454c46
2:/bin/yes
I would use regex for this as well:
The text file:
$ cat tst.txt
1234567890x1fgg0x1cfffrr
A script you can easily change/extend yourself.
#! /bin/bash
part="$(perl -0pe 's/^((?:(?!0(x|X)[0-9a-fA-F]+).)*)(0(x|X)[0-9a-fA-F]+)(.|\n)*/\1:\3\n/g;' tst.txt)"
tmp=${part/:0x*/}
tmp=${#tmp}
echo ${part/*:0x/$tmp:0x} # Echoes 123456789:0x1f
Regex:
^((?:(?!0x[0-9a-fA-F]+).)*) = Search for the first entry that's a hexadecimal number and create a group of it (\1).
(0x[0-9a-fA-F]+) = Make a group of the hexadecimal number (\3).
(.|\n)* = Whatever follows.
Please note that tmp=${part/:0x*/} could cause problems if you have text like :0x before the hexadecimal number that is caught.

Change the path address in a text file by shell scripting [closed]

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
In my Bash script, I have to change a name to a path address(new address) in a text file:
(MYADDREES) change to ( /home/run1/c1 ) and save it as new file.
I did like this: defined a new variable = new address and tried to replace it in previous address in text file.
I use sed but it has problem.
My script was:
#!/bin/bash
# To debug
set -x
x=`pwd`
echo $x
sed "s/MYADDRESS/$x/g" < sample1.txt > new.txt
exit
The output of pwd is likely to contain / characters, making your sed expression look something like s/MYADDRESS//home/user/somewhere/. This makes it impossible for sed to sort out what should be replaced with what. There are two solutions:
Use a different delimiter for sed:
sed "s,MYADDRESS,$x,g" < sample1.txt > new.txt
...although this will have the same problem if the current path contains a comma character or something else that is a special character for sed, so the more robust approach is to use awk instead:
awk -v curdir="$(pwd)" '{ gsub("MYADDRESS", curdir); print }' < sample1.txt > new.txt

Resources