Docker 容器在管道(grep、head、sed、awk)中没有相同的行为

Docker container doesn't have the same behaviour in Pipeline(grep,head,sed,awk)

我有一个 docker 容器(docker:stable),我想在其中获取 pom.xml 文件的版本 使用命令:

grep version pom.xml | grep -v -e '<?xml|~'| head -n 1 | sed 's/[[:space:]]//g' | sed -E 's/<.{0,1}version>//g' | awk '{print }'

它打印出我需要的东西2021.1.13-SNAPSHOT

现在,如果我在 gitlab 管道中使用相同的图像做同样的事情,我会得到

<?xmlversion="1.0"encoding="UTF-8"?>

怎么来的?有人知道这种行为吗?

根据 RavinderSingh13 的要求,这里 pom.xml 有问题

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>at.hello</groupId>
    <artifactId>kanutr-3IT</artifactId>
    <version>2021.1.13-SNAPSHOT</version>
    <name>kanuTR_3IT</name>

    <organization>
        <name>hello GmbH</name>
        <url>https://www.hello.at/</url>
    </organization>

    <repositories>
        <repository>
            <id>hello-repo</id>
            <name>company internal artifacts</name>
            <url>https://services.hello.at/maven/hello-repo</url>
        </repository>
        <repository>
            <id>hello-extern</id>
            <name>company extern artifacts</name>
            <url>https://services.hello.at/maven/hello-extern</url>
        </repository>
    </repositories>

    <properties>
        <kanutr.version>2022.1.2-SNAPSHOT</kanutr.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.3.8.RELEASE</spring.version>
        <maven-jar-plugin.mainClass>de.hello.balu.Server</maven-jar-plugin.mainClass>
        <maven-compiler-plugin.source>1.8</maven-compiler-plugin.source>
        <maven-compiler-plugin.target>1.8</maven-compiler-plugin.target>

        <mapping-scripts.outputDirectory>${project.build.directory}/mapping_scripts</mapping-scripts.outputDirectory>
        <mapping-scripts.inputDirectory>${project.build.scriptSourceDirectory}/mapping_scripts</mapping-scripts.inputDirectory>
        <resources.outputDirectory>${project.build.directory}/resources</resources.outputDirectory>
        <resources.inputDirectory>${project.basedir}/src/main/roll-out-resources</resources.inputDirectory>
    </properties>

    <dependencies>
        <!-- Company intern dependencies -->
        <dependency>
            <groupId>at.hello</groupId>
            <artifactId>kanutr</artifactId>
            <version>${kanutr.version}</version>
        </dependency>
        <dependency>
            <groupId>at.hello</groupId>
            <artifactId>kanutr</artifactId>
            <version>${kanutr.version}</version>
            <classifier>tests</classifier>
            <scope>test</scope>
        </dependency>
        <!-- Dependency for mapping scripts in kanuTR project -->
        <dependency>
            <groupId>at.hello</groupId>
            <artifactId>kanutr</artifactId>
            <version>${kanutr.version}</version>
            <classifier>mappings</classifier>
            <type>tar.gz</type>
            <scope>provided</scope>
        </dependency>
        <!-- Dependency for some resources (Regis-TR XSDs) in kanuTR project -->
        <dependency>
            <groupId>at.hello</groupId>
            <artifactId>kanutr</artifactId>
            <version>${kanutr.version}</version>
            <classifier>resources</classifier>
            <type>tar.gz</type>
            <scope>provided</scope>
        </dependency>
        <!-- Dependency for scripts in kanuTR project -->
        <dependency>
            <groupId>at.hello</groupId>
            <artifactId>kanutr</artifactId>
            <version>${kanutr.version}</version>
            <classifier>scripts</classifier>
            <type>tar.gz</type>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.10</version>
            <scope>test</scope>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.mockito</groupId>-->
<!--            <artifactId>mockito-core</artifactId>-->
<!--            <version>2.7.22</version>-->
<!--            <scope>test</scope>-->
<!--        </dependency>-->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                            <mainClass>${maven-jar-plugin.mainClass}</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>${maven-compiler-plugin.source}</source>
                    <target>${maven-compiler-plugin.target}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20</version>
                <configuration>
                    <forkCount>0</forkCount>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>

                <configuration>
                    <tarLongFileMode>posix</tarLongFileMode>
                    <descriptors>
                        <descriptor>src/assembly/roll-out.xml</descriptor>
                        <descriptor>src/assembly/roll-out-mifid.xml</descriptor>
                    </descriptors>
                </configuration>

                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>

            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.0.2</version>
                <executions>
                    <execution>
                        <id>unpack</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>at.hello</groupId>
                                    <artifactId>kanutr</artifactId>
                                    <classifier>mappings</classifier>
                                    <type>tar.gz</type>

                                    <includes>registr/**,deboe_apa/**</includes>
                                    <outputDirectory>${mapping-scripts.outputDirectory}</outputDirectory>
                                    <overWrite>true</overWrite>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>at.hello</groupId>
                                    <artifactId>kanutr</artifactId>
                                    <classifier>resources</classifier>
                                    <type>tar.gz</type>

                                    <outputDirectory>${resources.outputDirectory}</outputDirectory>
                                    <overWrite>true</overWrite>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${resources.outputDirectory}</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${resources.inputDirectory}</directory>
                                </resource>
                            </resources>
                            <overwrite>true</overwrite>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-scripts</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${mapping-scripts.outputDirectory}</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${mapping-scripts.inputDirectory}</directory>
                                </resource>
                            </resources>
                            <overwrite>true</overwrite>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

第一个解决方案: 使用您显示的示例,请尝试遵循 GNU awk 代码。在 GNU awk.

中编写和测试
awk '
/<\?xml version="1\.0" encoding="UTF-8"\?>/{
  found=1
  next
}
found && match([=10=],/^[[:space:]]*<version>([^<]*)<\/version>/,arr){
  print arr[1]
  exit
}
' Input_file


第二个解决方案:应该适用于任何版本的awk

awk '
/<\?xml version="1\.0" encoding="UTF-8"\?>/{
  found=1
  next
}
found && match([=11=],/^[[:space:]]*<version>[^<]*<\/version>/,arr){
  val=substr([=11=],RSTART,RLENGTH)
  gsub(/.*<version>|<\/version>/,"",val)
  print val
  exit
}
'  Input_file


第一个解决方案的解释:为上面添加详细解释。

##Starting awk program from here.
awk '
##Checking condition if line contains <?xml version="1.0" encoding="UTF-8"?> then do following.
/<\?xml version="1\.0" encoding="UTF-8"\?>/{
##Setting found variable value to 1 here.
  found=1
##next will skip all further statements from here.
  next
}
##Checking condition if found is SET and using match function to match regex ^[[:space:]]*<version>([^<]*)<\/version> where needed value SNAPSHOT value is stored in array arr's 1st value.
found && match([=12=],/^[[:space:]]*<version>([^<]*)<\/version>/,arr){
##Printing array arr's 1st value here.
  print arr[1]
##Exitig from here, to save some cycles and time.
  exit
}
' Input_file ##Mentioning Input_file name here.