Cut & awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Cut & awk
# 1  
Old 07-20-2015
Cut & awk

I am using the following code to modify all odd lines in a file:
Code:
awk 'NR % 2 { print } !(NR % 2)' FWD-1.fas | cut -c5-600

I however, do not want cut to affect the odd lines
Any help?
# 2  
Old 07-20-2015
Don't use cut. awk equivalent is substr($0,5,595)

try:
Code:
awk '!(NR%2) { print substr($0,5,595); next; } 1' FWD-1.fas

This User Gave Thanks to neutronscott For This Post:
# 3  
Old 07-20-2015
Hi Xterra,
The pipeline you have is logically equivalent to:
Code:
cut -c5-600 FWD-1.fas

but runs slower since you're reading and writing the file twice instead of just once.

Hi Neutronscott,
I think you have an off-by-1 error in the substring. What you have would emulate cut -c5-599

I would try something like:
Code:
awk '!(FNR % 2) {$0 = substr($0, 5, 596)} 1' FWD-1.fas

These 2 Users Gave Thanks to Don Cragun For This Post:
# 4  
Old 07-20-2015
oy. darn 1-index. Thanks, Don.

but yes, everything outputted by awk goes to cut. cut doesn't have logic like awk does. But awk can determine if it's an odd line and also skip the first 5 characters.

the 1 at the end is shorthand for {print $0}, so that lines that weren't modified will still be printed as they were.
This User Gave Thanks to neutronscott For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut & Fetch word from string

I have a file with some SQL query, I want to fetch only Table Name from that file line by line. INPUT FILE SELECT * FROM $SCHM.TABLENAME1; ALTER TABLE $SCHM.TABLENAME1 ADD DateOfBirth date; INSERT INTO $SCHM.TABLENAME1 (CustomerName, Country) SELECT SupplierName, Country FROM $SCHM.TABLENAME2... (2 Replies)
Discussion started by: Pratik Majithia
2 Replies

2. Homework & Coursework Questions

sed & cut command issues

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: After using the egrep command to pull certain lines from the asg5f1 (creating the asg5f1c file), I am required... (1 Reply)
Discussion started by: robrom78
1 Replies

3. Shell Programming and Scripting

cut & AWK

HI all, i have the source file like below, 05 BL-FEE-CYC-CDE PIC S9(03) COMP-3. 05 BL-FEE-ERR-MSG PIC X(00030). 05 BL-FEE-TYPE PIC X(00001). 418181*# 05 ... (7 Replies)
Discussion started by: baskivs
7 Replies

4. Shell Programming and Scripting

Advice using cut & echo combination commands

Hi, I am cutting data from a fixed length test file and then writing out a new record using the echo command, the problem I have is how to stop multiple spaces from being written to the output file as a single space. Example: cat filea | while read line do field1=`echo $line | cut -c1-2` ... (6 Replies)
Discussion started by: dc18
6 Replies

5. Shell Programming and Scripting

storing output from echo & cut into variable

Hi All, Hope someone can advise here as I have been struggling to find a syntax that works here. I have tried a stack of combination I have seed in the forums but I think because I have needed to use "" and `` in the statments another method is found. I am reading in lines with the following... (1 Reply)
Discussion started by: nkwilliams
1 Replies

6. UNIX for Dummies Questions & Answers

cmd sequence to find & cut out a specific string

A developer of mine has this requirement - I couldn't tell her quickly how to do it with UNIX commands or a quick script so she's writing a quick program to do it - but that got my curiousity up and thought I'd ask here for advice. In a text file, there are some records (about half of them)... (4 Replies)
Discussion started by: LisaS
4 Replies

7. Shell Programming and Scripting

grep & cut the file

I need to grep and cut the some file in the folder and output to some file. the sample file looks like below -rw-r--r-- 1 gui gui 28050789 Jun 25 18:38 mymkzpiii.txt -rw-r--r-- 1 gui gui 127983856 Jun 25 18:39 phmnlpiii.txt i need the output like below ... (3 Replies)
Discussion started by: aboorkuma
3 Replies

8. Shell Programming and Scripting

awk & cut record separator problem

Hi All, I've got some strange behaviour going on when trying to manipulate a file that contains spaces. My input file looks something like this: xxxxxxxxx,yyyy,sss sss sss,bbbbbbb If I use awk: When running from the command line I get: sss sss sss But when running from a... (7 Replies)
Discussion started by: pondlife
7 Replies

9. Shell Programming and Scripting

cut & paste

hi i am new to shell scripting, i have been trying to cut columns numbered 1,4 of a file consisiting of 4 columns. Each column is seperated by 2 spaces. for example: john 6102097199 tennessee usa michel 6734590899 texas USA now, i need to cut the name... (3 Replies)
Discussion started by: t_harsha18
3 Replies
Login or Register to Ask a Question