Visit Our UNIX and Linux User Community


File formating help

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers File formating help
# 1  
Old 01-24-2017
File formating help

Hi all,

I am having the file below

Quote:
"NOT APPLICABLE","0000103","NOT APPLICABLE","Y"
"NOT APPLICABLE","0000109","NOT APPLICABLE","Y"
"NOT APPLICABLE","0005004","NOT APPLICABLE","Y"
"NOT APPLICABLE","0000001","NOT APPLICABLE","Y"
"NOT APPLICABLE","0000002","NOT APPLICABLE","Y"
I need that as below

Quote:
"NOT APPLICABLE","0000103","1","Y"
"NOT APPLICABLE","0000109","1","Y"
"NOT APPLICABLE","0005004","1","Y"
"NOT APPLICABLE","0000001","1","Y"
"NOT APPLICABLE","0000002","1","Y"
Thanks,
Arun
# 2  
Old 01-24-2017
what have you tried so far and where exactly are you stuck?
# 3  
Old 01-24-2017
I am able to get the position value by below command and not able to transform that to 1 and replace in file

Code:
cut -d, -f3 S2007080A.011317.0000043042.SUPP.CSVX

# 4  
Old 01-24-2017
Quote:
Originally Posted by arunkumar_mca
I am able to get the position value by below command and not able to transform that to 1 and replace in file

Code:
cut -d, -f3 S2007080A.011317.0000043042.SUPP.CSVX

I've noticed there're a number of awk-based proposed solutions posted for your 336 posts.
Could you try something with awk and see how far you get, please!
This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 01-24-2017
Able to extract the value using AWK but not able to replace that in the file

Code:
awk -F, '{print $3=1}' FSDS.CSVX

Below also not working
Code:
 awk 'BEGIN{OFS=FS=","}$3=="NOT APPLICABLE"{$3=1}{print}' file
 awk -F "|" '{ if ( $3 =="NOT APPLICABLE" ) print $1"|"$2"|"1"|$4' file


Last edited by arunkumar_mca; 01-24-2017 at 12:42 PM.. Reason: added command
# 6  
Old 01-24-2017
Files do not work that way. You can't write to the same file you're reading from. A few commands can pretend, but they're really using temp or whole-memory buffers files like everything else does:
Code:
awk ... filename > /tmp/$$
# BACK UP YOUR DATA before doing this!  One mistake and filename's contents are blown away
cat /tmp/$$ > filename
rm -f /tmp/$$

# 7  
Old 01-24-2017
Quote:
Originally Posted by arunkumar_mca
Able to extract the value using AWK but not able to replace that in the file

Code:
awk -F, '{print $3=1}' FSDS.CSVX

Below also not working
Code:
 awk 'BEGIN{OFS=FS=","}$3=="NOT APPLICABLE"{$3=1}{print}' file
 awk -F "|" '{ if ( $3 =="NOT APPLICABLE" ) print $1"|"$2"|"1"|$4' file

awk doesn't provide the facility to edit "in-place". redirect to a temp file and then mv.
Make sure you provide a correct field separator.

Previous Thread | Next Thread
Test Your Knowledge in Computers #203
Difficulty: Medium
Perl is not an interpreted language.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with ... Formating the file using awk

I have a file like below position1 0 7802 7802 0 client1 - - position1 8 8032 8032 0 client1 ... (7 Replies)
Discussion started by: manas_ranjan
7 Replies

2. Shell Programming and Scripting

File formating

I need to create a fixed width file based on the column lengths. lets assume I have six(this may be dynamic) fields each are of different length column1=6 #size of the column column2=3 column3=2 column4=3 column5=4 column6=5 I tried below code snippet but it is not working echo... (4 Replies)
Discussion started by: gvkumar25
4 Replies

3. UNIX for Beginners Questions & Answers

File formating with lines

Hi All, Need to modify a file basically join the extra line to one I am having a file like below And would like to make it as below . (4 Replies)
Discussion started by: arunkumar_mca
4 Replies

4. Shell Programming and Scripting

Help in formating a file with field separator

hi thanks a lot But pls help me if my input file is the below format: Name priya 2010-09-21 10:43:49 TEXT ID 1 hi TEXT ID 2 how TEXT ID 3 r TEXT ID 4 u Name2 priya2 2010-09-21 10:43:49 TEXT ID 1 hi1 TEXT ID 2 how1 TEXT ID 3 r1 (4 Replies)
Discussion started by: bha148
4 Replies

5. Shell Programming and Scripting

Help in formating a txt file

Pls help in formatting a txt file using shell scripting Input file format: Name priya 2010-09-21 10:43:49 TEXT ID 1 hi TEXT ID 2 how TEXT ID 3 r TEXT ID 4 u Output required: name priya hi how r u (4 Replies)
Discussion started by: bha148
4 Replies

6. Shell Programming and Scripting

File formating (sed command)

I am new ot unix. Is there one or two lines of command (sed may be) to format a file. My source is.. <tag1> <tag2> <datatag1> data1 </datatag1> <datatag2> data2 </datatag2> <datatag3> data3 </datatag3> <datatag4> data1 </datatag3> </tag2> </tag1> (2 Replies)
Discussion started by: reachsam11
2 Replies

7. UNIX for Dummies Questions & Answers

File formating help needed.

I have a file called vm.cfg . content of file. acpi = 1 apic = 1 builder = 'hvm' How would i write a script which will add boot = 'd' at 3rd line. For example. after running the script the file should be like below. acpi = 1 apic = 1 boot = 'd' builder = 'hvm' I dont want to... (3 Replies)
Discussion started by: pinga123
3 Replies

8. Shell Programming and Scripting

file formating in Perl

Hi, I am new to unix , I have a requirement for formating the input file and generate the output file as per the downstream requirement .. My application receiving a text input file having 4 field and my application need to check each field and if some value of a field is blank ..then it need... (1 Reply)
Discussion started by: julirani
1 Replies

9. Shell Programming and Scripting

File formating question.

Hi, I have a file with only data 1 row: AA#?BB#?CC#?DD Assume '#?' is the delimiter, i want the output in another file to be: AA BB CC DD Pls let me know if this is possible by sed ? Thanks in advance. (3 Replies)
Discussion started by: smc3
3 Replies

10. UNIX for Dummies Questions & Answers

Help required on file formating

I have two files file1 and file2 as shown below: file1: name nameabc bcd nameabcdefg file2: age age1111 age2345 age6743 I have pasted one file on the other with the delimiter "|" and the resulttant file is: name|age nameabc|age1111 bcd|age2345 nameabcdefg|age6743 (1 Reply)
Discussion started by: udiptya
1 Replies

Featured Tech Videos