Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

bash return every nth line

# Basic syntax:
awk '(FNR%4==0){print substr($column_number, start_character, number_characters);next} 1' your_file.txt > trimmed_file.txt
# Where:
#	- FNR is the current file record number (typically the line number)
#	- (FNR%4==0) checks whether the current line number is evenly divisible by
#		4, to effectively trim every 4th row
#	- For line numbers that are evenly divisible by 4, trim them with substr:
#		- column_number is the field for which you want a substring
#		- start_character is the number of the first character from the left
#			(inclusive) that you want to incude in the substring
#		- number_characters is the number of characters to print past the 
#			start character (inclusive)
#		- next skips all further statements in the awk string, causing awk
#			to move on to the next line of the file
#	- For line numbers that aren't evenly divisible by 4:
#		- 1 causes the line to be printed as is (same as $0)

# Example usage:
# Say you have a file with the following contents and want to trim every
# second and fourth line to a length of 30
@SRR001666.1 071112_SLXA-EAS1_s_7:5:1:817:345 length=72
GGGTGATGGCCGCTGCCGATGGCGTCAAATCCCACCAAGTTACCCTTAACAACTTAAGGGTTTTCAAATAGA
+SRR001666.1 071112_SLXA-EAS1_s_7:5:1:817:345 length=72
IIIIIIIIIIIIIIIIIIIIIIIIIIIIII9IG9ICIIIIIIIIIIIIIIIIIIIIDIIIIIII>IIIIII/
@SRR001666.2 071112_SLXA-EAS1_s_7:5:1:801:338 length=72
GTTCAGGGATACGACGTTTGTATTTTAAGAATCTGAAGCAGAAGTCGATGATAATACGCGTCGTTTTATCAT
+SRR001666.2 071112_SLXA-EAS1_s_7:5:1:801:338 length=72
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII6IBIIIIIIIIIIIIIIIIIIIIIIIGII>IIIII-I)8I

# Running:
awk '(FNR%2==0 || FNR%4==0){print substr($0,1,30);next} 1' your_file.txt > output.txt

# Would produce:
@SRR001666.1 071112_SLXA-EAS1_s_7:5:1:817:345 length=72
GGGTGATGGCCGCTGCCGATGGCGTCAAAT
+SRR001666.1 071112_SLXA-EAS1_s_7:5:1:817:345 length=72
IIIIIIIIIIIIIIIIIIIIIIIIIIIIII
@SRR001666.2 071112_SLXA-EAS1_s_7:5:1:801:338 length=72
GTTCAGGGATACGACGTTTGTATTTTAAGA
+SRR001666.2 071112_SLXA-EAS1_s_7:5:1:801:338 length=72
IIIIIIIIIIIIIIIIIIIIIIIIIIIIII
# Note, the length=72 is no longer accurate after trimming
Comment

PREVIOUS NEXT
Code Example
Shell :: create group ubuntu 
Shell :: mkdir create if not exists 
Shell :: npm ERR! path /usr/local/lib/nodejs/node-v10.15.3-linux-x64/lib/node_modules while installing angular cli 
Shell :: how do i update ubuntu 
Shell :: packet tracer linux mint 
Shell :: delete mulitple git branch 
Shell :: insta hack 
Shell :: how to remove a remote origin in git and add new 
Shell :: heroku bash 
Shell :: redis-cli port host 
Shell :: git blame before specific commit 
Shell :: open xampp control panel from terminal ubuntu 20 
Shell :: ssh pc without password ubuntu 
Shell :: vercel 
Shell :: powershell create symlink 
Shell :: ubuntu mouse cursor disappears 
Shell :: wasm pack install 
Shell :: how to find my apache server ip address 
Shell :: the digital freelance company 
Shell :: npx pod update 
Shell :: shared folder virtualbox ubuntu 
Shell :: foreach powershell 
Shell :: see changes to be pushed git 
Shell :: install android repo 
Shell :: connect to wifi via Terminal 
Shell :: tbomb github 
Shell :: how to check whether git is initialized or not 
Shell :: pgadmin4 container 
Shell :: join docker swarm 
Shell :: powershell make file 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =