Create a file on UNIX with multiple columns on certain condition


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Create a file on UNIX with multiple columns on certain condition
# 1  
Old 05-29-2014
Create a file on UNIX with multiple columns on certain condition

I need to write the list of files to a new file in one column , the second column would contain the first line of that file (header record extracted through head -1 ) and the third column would contain the last record of that file (trailer record tail -1 ) .
Example :- folder where the files exists : /tmp/multiple files List of files : file1 , file2 , file3 , file 4
Need to create a file xyz.hello at new folder /tmp/newfolder

file1 "header record of file1" "last line of file1"
file2 "header record of file2" "last line of file2"
file3 "header record of file3" "last line of file3"
file4 "header record of file4" "last line of file4
# 2  
Old 05-29-2014
IshuGupta, what have you tried so far?
# 3  
Old 05-29-2014
I am beginner to unix so I am having a hard time writing code , as of now I have just wrote down a sample to populate 2 colums , this too is not working .

Code:
#!/bin/bash
find /splunk/scrubbed/triumph/ -mtime 1 > /tmp/try/balancing
file = "/tmp/try/balancing"
#field1 = 'head -1'
while read file
do
    echo "head -1 $LINE" >> temp_file.txt
done < balancing1
temp_file.txt > balancing1


Last edited by Don Cragun; 05-29-2014 at 10:04 PM.. Reason: Add CODE tags.
# 4  
Old 05-29-2014
Does it do what you want?

Code:
#!/bin/bash

cd /splunk/scrubbed/triumph/

for f in *; do
    [[ -f $f ]]  && printf "%s \"%s\" \"%s\"\n" ${f} "$(head -1 ${f})" "$(tail -1 ${f})"
done

# 5  
Old 05-29-2014
Is this a homework assignment?

Moderator's Comments:
Mod Comment (Note: Aia's response has been hidden until we get a response to this question.)


If not, please explain why you need to do this.

Last edited by Don Cragun; 05-29-2014 at 10:12 PM.. Reason: Add note about hidden response.
# 6  
Old 05-29-2014
Thanks alot for replying , It is not what i want ..but it is a good head start...

I just want to head and tail the record of the first column (which are file name itself) of file "balancing" in the below code , not the balancing file itself .
For example , if the file contains 3 records as
abc
pqr
xyz

then I want to create a file with three colums like below like

abc <result of tail -1 abc > <result of head -1 abc>
pqr <result of tail -1 pqr> <result of head -1 pqr>
xyz <result of tail -1 xyz> <result of head -1 xyz>

The below code is giving me the result of balancing file first and last record only .

Code:
#!/
#!/bin/bash
for f in /tmp/try/balancing; do
    [[ -f $f ]]  && printf "%s \"%s\" \"%s\"\n" ${f} "$(head -1 ${f})" "$(tail -1 ${f})"
done

---------- Post updated at 08:21 PM ---------- Previous update was at 08:19 PM ----------

It is not a home work assignment , I need to create a file based on all the files that we are getting in our unix box each day, that shows the first and last record of each file based , so that we can validate the information in the same.
# 7  
Old 05-29-2014
Sorry! I recognize I did not look hard enough to what you wanted and I based my answer on your first post.


Trying to reuse your code. I haven't test it.
Code:
#!/bin/bash

find /splunk/scrubbed/triumph/ -type f -mtime 1 > /tmp/try/balancing
list="/tmp/try/balancing"

while read file
do
    $first_line=$(head -1 ${file})
    $last_line=$(tail -1 ${file})
    printf "%s: \"%s\" \"%s\"\n" "${file}" "${first_line}" "${last_line}" >> temp_file.txt
done <  $list

#what's balancing1?
#temp_file.txt > balancing1

---------- Post updated at 07:40 PM ---------- Previous update was at 07:26 PM ----------

Code:
#!/bin/bash
for f in /splunk/scrubbed/triumph/*; do
    [[ -f $f ]]  && printf "%s \"%s\" \"%s\"\n" ${f} "$(head -1 ${f})" "$(tail -1 ${f})"
done

To give it a chance to that code you need to change to the red highlight.
It needs to iterate through the same directory that you use in the find command. I was not trying to read from a file. I was trying to read the content of a directory.
This User Gave Thanks to Aia 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

Split into multiple files by using Unique columns in a UNIX file

I have requirement to split below file (sample.csv) into multiple files by using the unique columns (first 3 are unique columns) sample.csv 123|22|56789|ABCDEF|12AB34|2019-07-10|2019-07-10|443.3400|1|1 123|12|5679|BCDEFG|34CD56|2019-07-10|2019-07-10|896.7200|1|2... (3 Replies)
Discussion started by: RVSP
3 Replies

2. UNIX for Beginners Questions & Answers

Need help search multiple condition from a file.

OS: window 7 shell : korn shell I have 2 file , i'm need grep data according File_1 from file 2. File_1 CAL_ENAB_N_4_ $2N12743_29 +12V File_2 NODE CAL_ENAB_N_4_ PINS 21548; PROBES P1465 3651, 46900 tn2700.1 LWT; WIRES (6 Replies)
Discussion started by: kttan
6 Replies

3. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns satisfy the condition

HI All, I'm embedding SQL query in Script which gives following output: Assignee Group Total ABC Group1 17 PQR Group2 5 PQR Group3 6 XYZ Group1 10 XYZ Group3 5 I have saved the above output in a file. How do i sum up the contents of this output so as to get following output: ... (4 Replies)
Discussion started by: Khushbu
4 Replies

4. Shell Programming and Scripting

Create a UNIX script file with multiple commands

Hi Good morning all, I want to create script file with multiple commands. For ex: pmrep connect is one of the command to connect to repository. pmrep objectexport is another command to export objects to a file. These commands should run sequentially.But when i try to execute this, the first... (4 Replies)
Discussion started by: SekhaReddy
4 Replies

5. Shell Programming and Scripting

Comparing Select Columns from two CSV files in UNIX and create a third file based on comparision

Hi , I want to compare first 3 columns of File A and File B and create a new file File C which will have all rows from File B and will include rows that are present in File A and not in File B based on First 3 column comparison. Thanks in advance for your help. File A A,B,C,45,46... (2 Replies)
Discussion started by: ady_koolz
2 Replies

6. Shell Programming and Scripting

Create Multiple UNIX Files for Multiple SQL Rows output

Dear All, I am trying to write a Unix Script which fires a sql query. The output of the sql query gives multiple rows. Each row should be saved in a separate Unix File. The number of rows of sql output can be variable. I am able save all the rows in one file but in separate files. Any... (14 Replies)
Discussion started by: Rahul_Bhasin
14 Replies

7. Shell Programming and Scripting

Sort based on Multiple Columns in UNIX

Hi, I would like to sort a list in different ways: 1> Unique based on Field 1 with highest Field 4 For Instance Input: 1678923450;11112222333344;11-1x;2_File.xml 1678923450;11112222333344;11-1x;5_File.xml 1234567890;11113333222244;11-1x;3_File.xml Output: ... (7 Replies)
Discussion started by: DevendraG
7 Replies

8. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

9. Shell Programming and Scripting

create separate file after checking condition..

Problem : I want to create a separate file for country list if condition is true. Please help. ***************************************************** Input file: SV-INCR-139302-365540488-201104090934.sqllog SV-INCR-1082-552793184-201104040805.sqllog SV-INCR-1077-855045741-201104040805.sqllog... (4 Replies)
Discussion started by: humaemo
4 Replies

10. Shell Programming and Scripting

Compare columns of 2 files based on condition defined in a different file

I have a control file which tells me which are the fields in the files I need to compare and based on the values I need to print the exact value if key =Y and output is Y , or if output is Y/N then I need to print only Y if it matches or N if it does not match and if output =N , then skip the feild... (7 Replies)
Discussion started by: newtoawk
7 Replies
Login or Register to Ask a Question