Optimize a combination of commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Optimize a combination of commands
# 1  
Old 11-24-2017
Optimize a combination of commands

Im currently running this command to satisfy a particular task. it works for my purposes. but i want to be able to optimize this string of commands and have it be reduced to 1 or 2 commands, if at all possible:


Code:
head -4 datafile 2>/dev/null | cut -c1-400 | wc | awk '{print $2$1$3}'


Last edited by SkySmart; 11-24-2017 at 08:20 PM..
# 2  
Old 11-24-2017
I don't see why a single awk command couldn't do all of that, although whether it would be quicker, I doubt.

And wc only returns 4 fields, at least as far as I see, so not sure what $5 in your awk gives you.
# 3  
Old 11-24-2017
Not sure that pipe makes any sense at all. wc in a pipe prints three fields (four if operating on file parameters), so the following awk doesn't have a $5 to print.
WHAT exactly do you want to achieve?
# 4  
Old 11-24-2017
the
Code:
$5

was meant to be
Code:
$2

. I have changed it.
# 5  
Old 11-24-2017
Quote:
Originally Posted by SkySmart
the
Code:
$5

was meant to be
Code:
$2

. I have changed it.
That doesn't answer the questions that have been asked. What are you trying to do?

In what way does converting output from wc, such as:
Code:
   18428  115732  794534

into:
Code:
11573218428794534

perform any useful purpose?
# 6  
Old 11-25-2017
Just for the fun of it - as the original spec for me is unintelligible - this would yield the same, incomprehensible result:
Code:
awk '{$0 = substr ($0,1,400); TNF += NF; TC += length} NR==4 {exit} END {print TNF NR TC+NR}' datafile

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. Shell Programming and Scripting

awk or a combination of commands to read and calculate nth lines from pattern

Two numerical lines, found by either header line, need to be added and the total placed in a new-header section. Also the total should should be rounded or cut to a two decimal anynumber.XX format with the AB string added on the end. For example: The numerical lines from headers 2 and 3 are... (3 Replies)
Discussion started by: jessandr
3 Replies

2. UNIX for Beginners Questions & Answers

Extract delta records using with "comm" and "sort" commands combination

Hi All, I have 2 pipe delimited files viz., file_old and file_new. I'm trying to compare these 2 files, and extract all the different rows between them into a new_file. comm -3 < sort file_old < sort file_new > new_file I am getting the below error: -ksh: sort: cannot open But if I do... (7 Replies)
Discussion started by: njny
7 Replies

3. Shell Programming and Scripting

Help Optimize the Script Further

Hi All, I have written a new script to check for DB space and size of dump log file before it can be imported into a Oracle DB. I'm relatively new to shell scripting. Please help me optimize this script further. (0 Replies)
Discussion started by: narayanv
0 Replies

4. Shell Programming and Scripting

Looking to optimize code

Hi guys, I feel a bit comfortable now doing bash scripting but I am worried that the way I do it is not optimized and I can do much better as to how I code. e.g. I have a whole line in a file from which I want to extract some values. Right now what I am doing is : STATE=`cat... (5 Replies)
Discussion started by: Junaid Subhani
5 Replies

5. Shell Programming and Scripting

Optimize the nested IF

Hi, I have to assign a value for a varaiable based on a Input. I have written the below code: if then nf=65 elif then nf=46 elif then nf=164 elif then nf=545 elif then nf=56 elif then (3 Replies)
Discussion started by: machomaddy
3 Replies

6. 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

7. Shell Programming and Scripting

can we optimize this command

can we optimize this command ? sed 's#AAAA##g' /study/i.txt | sed '1,2d' | tr -d '\n\' > /study/i1.txt; as here i am using two files ...its overhead..can we optimise to use only 1 file sed 's#AAAA##g' /study/i.txt | sed '1,2d' | tr -d '\n\' > /study/i.txt; keeping them same but it... (9 Replies)
Discussion started by: crackthehit007
9 Replies

8. UNIX for Dummies Questions & Answers

combination of two commands

I want to show a output like this Lee Ballancore PID TTY TIME CMD 31799 pts/3 00:00:00 vim 31866 pts/3 00:00:00 vim 2495 pts/7 00:00:00 vim 8368 pts/0 00:00:00 vim 9544 pts/2 00:00:00 ps Alistairr Rutherford PID TTY TIME CMD 8368 pts/0 00:00:00 vim 9544 pts/2 00:00:00 ps ... (3 Replies)
Discussion started by: nehaquick
3 Replies

9. Shell Programming and Scripting

optimize the script

Hi, I have this following script below. Its searching a log file for 2 string and if found then write the strings to success.txt and If not found write strings to failed.txt . if one found and not other...then write found to success.txt and not found to failed.txt. I want to optimize this... (3 Replies)
Discussion started by: amitrajvarma
3 Replies

10. UNIX for Dummies Questions & Answers

Combination Of commands

Hello All, I just wanted to know what are the different ways of using commands in combination. The most common one which i know is using pipes. Also grouping is also done like ( ls; date) where output of both the commands is displayed. Are there any other ways of combining various... (2 Replies)
Discussion started by: rahulrathod
2 Replies
Login or Register to Ask a Question