How To Write my Bash Script To Automate it?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How To Write my Bash Script To Automate it?
# 1  
Old 02-16-2019
How To Write my Bash Script To Automate it?

Hello guys, I need some help. I am new in bash and I don't know how to automate the following script.


Code:
head -2 out1 > 1.fasta
sed ‘1,2 d' out1 > out2
rm out1
head -2 out2 > 2.fasta
sed ‘1,2 d' out2 > out1
rm out2
head -2 out2 > 3.fasta
sed '1,2 d' out2 > out1
rm out2
.......
......

my file has 69800 lines and I want to do that automatically.


thanks a lot
laura
# 2  
Old 02-16-2019
Welcome to the forum.


If you post a question, it's best to also mention the OS, shell, and tools' versions that you use, so a solution can be taylored to your situation. For instance, several seds out there have features that allow for a concise solution that others don't.

- Do you have awk at hand? Could be used to do all of it in one go.
- Did you consider the split command? Looks like it is made for your problem, even to produce the desired file names.
# 3  
Old 02-16-2019
Hi RudiC,
The standard split utility creates output filenames with sequence "numbers" added to the end of a given output filename prefix where the "numbers" aren't numeric; they are names like fasta.aa, fasta.ab, fasta.ac, ...

Like you said, awk would work well for this.

Hi laura,
To get the requested output, one could use something like:
Code:
#!/bin/bash
awk 'NR % 2 {	f = ++cnt".fasta" }
{		print > f }
!(NR % 2){	close(f)}
' out1 && rm out1

but that would produce names with varying output lengths that would be inconvenient to process if you wanted to handle them in the same order that they originally appeared in the file named out1. If you do later want to process them in that order, you would probably be happier with something more like:
Code:
#!/bin/bash
awk 'NR % 2 {	f = sprintf("%05d.fasta", ++cnt) }
{		print > f }
!(NR % 2){	close(f)}
' out1 && rm out1

which would produce all 34900 output files with names that start with 5 decimal digits (with leading zeros on the first 9999 files so normal listings of the produced files are in the order you probably want.

If you intend to try this on a Solaris/SunOS operating system, change awk in both of the above suggestions to /usr/xpg4/bin/awk or nawk.

As RudiC said, next time you need help, plan to tell us what operating system as well as what shell you're using and show us what you have tried to solve the problem on your own. We want to help you learn how to do stuff like this on your own; not to act as your unpaid programming staff.
# 4  
Old 02-16-2019
Hi Don,


I had a comment on the non-fitting file names similar to what you said but removed it when I tested the split installed on my linux (split (GNU coreutils) 8.28):

Code:
$ split -dl2 --additional-suffix=.fasta file ""
$ ls -la
-rw-rw-r-- 1 user group  379 Feb 16 19:39 00.fasta
-rw-rw-r-- 1 user group  349 Feb 16 19:39 01.fasta
-rw-rw-r-- 1 user group  175 Feb 16 19:39 02.fasta

Had the requestor told us what system s/he has, a more specific answer had resulted.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to write a value to a physical memory address in bash script?

How would I write a value to a physical memory address? I was able to read a physical memory address (for example, 0x400) using this line: dd if=/dev/mem count=4 bs=1 skip=$(( 0x400 )) But I get an error: dd: 'standard input': cannot skip to specified offset when I try to write using... (1 Reply)
Discussion started by: rabrandt
1 Replies

2. UNIX for Beginners Questions & Answers

Automate "touch" bash script not working.

#!/bin/bash -i SAVEIFS=$IFS IFS=$"\n\b" picc=$* if ; then echo $TDATE if ; then touch dummy touch -t "tdate" dummy touch -r "dummy" "$picc" else echo -e "No mod date value to apply. If there is one in your shell,\ninvoke \eStarted asking advice on this (on Linuxquestions.org).... (9 Replies)
Discussion started by: iamwrong
9 Replies

3. Shell Programming and Scripting

Automate OTPW login to ssh via bash script

Hello everyone. I'm a Linux novice trying out a lot of bash scripting lately, as it is so very addictive. Lately I have been setting up one of my boxes remotely and have been hardening it as much as possible. Please allow me to explain the scenario, as it does tend to become a little... (1 Reply)
Discussion started by: instro
1 Replies

4. Shell Programming and Scripting

How to write bash script for lvm snapshot?

Hi Team I am trying to put together a nice small script to mount my lvm snapshot Here are my objectives 1 Check whether snapshot is currently mounted. If so echo umount snapshot and exit from the script. 2 If it's not mounting, check whether the mount point exist, If so, create lvm... (0 Replies)
Discussion started by: fugeulu
0 Replies

5. Shell Programming and Scripting

How to write bash shell script for mentioned requirement?

Hi All, I am unable to write the script for the below requirement. Requirement: Main table dir_ancillary table contain three column "dir_ancillary.table_name"," dir_ancillary.text_file_name ", "dir_ancillary.Include" . This dir_ancillary contain undefined tables in the column... (2 Replies)
Discussion started by: Vineeta Nigam
2 Replies

6. Shell Programming and Scripting

Bash script to Automate the Virtual Host creation process!!

Hi all, This is my sample code in /etc/httpd/conf.d/applications.conf file currently we are creating subdomain mannually for every new subdomain. I want to automate this process througs bash script , how its possible. <VirtualHost *:80> ServerName google.com ServerAlias google.com... (5 Replies)
Discussion started by: anishkumarv
5 Replies

7. Shell Programming and Scripting

Help to write bash script code

I am newbie to UNIX. I came across this exercise in one the books.Can anyone help me with this question?????? Write a short Bash script that, given the name of a file as an argument, reads the file name and creates a new file containing only lines which consist of one word. Here is an example... (4 Replies)
Discussion started by: krthknaidu
4 Replies

8. Homework & Coursework Questions

How to write script in bash.

I am very new to Linux/Unix. Kindly assist the following: I wish to write a bash shell script called how_many_to_go that calculates and prints the number of days, hours, minutes and/or seconds until the end of the current month (based on the output of the date command). Do ... (2 Replies)
Discussion started by: alobi
2 Replies

9. Shell Programming and Scripting

Bash Script to Read & Write on different directories

Hi, root@server] df -h 121G 14G 101G 12% /home 147G 126G 14G 91% /backup We having our site files and images are storing in /backup/home/user/files/ through symbolic link created in /home directory pointing in /backup directory as following. root@server] cd /home... (1 Reply)
Discussion started by: mirfan
1 Replies

10. Shell Programming and Scripting

Can anybody write this bash script ?

hi, first congratulations on the nice forum! Can anybody write script, which can make copy of some or all files of the current directory in new directory (called "backups", which must be made in the current directory, if it's not already exist). And bring out a massage (report) with the count... (7 Replies)
Discussion started by: Cecko
7 Replies
Login or Register to Ask a Question