Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

awk trim 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 :: bash limit memory use of a function 
Shell :: apartheid linux 
Shell :: remove all games ubuntu 
Shell :: install docker-compose ec2 
Shell :: bash: tree: command not found... centos7 
Shell :: how to remove every space in a string in bash 
Shell :: bash write file 
Shell :: install ionic cli specific version 
Shell :: kill process on port 
Shell :: update linux command 
Shell :: install nvm via npm 
Shell :: git remove last commit origin 
Shell :: count number of lines of code in git repo 
Shell :: debuild: command not found 
Shell :: update every python library 
Shell :: install neovim ubuntu 
Shell :: how to install mpv linux 
Shell :: linux kustomize install 
Shell :: wget recursive 
Shell :: how to uninitialize git 
Shell :: ubuntu capture screen 
Shell :: install heroku cli on linux 
Shell :: delete first column bash 
Shell :: replace first occurence of substring 
Shell :: The terminal process failed to launch: Path to shell executable "/bin/zsh" does not exist. 
Shell :: install java 16 ubuntu 
Shell :: git how to reset only one file 
Shell :: install fontforge linux 
Shell :: `Cannot autolaunch D-Bus without X11 $DISPLAY` 
Shell :: bash Creating help function 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =